antlr4 compile error: Serialized ATN data element out of range - java

antlr4.5, target Java, jdk1.6.
I compiled a .g4 combined file, and got this error:
Exception in thread "main" java.lang.UnsupportedOperationException: Serialized ATN data element out of range.
at org.antlr.v4.runtime.atn.ATNSerializer.serialize(ATNSerializer.java:370)
at org.antlr.v4.runtime.atn.ATNSerializer.getSerialized(ATNSerializer.java:547)
at org.antlr.v4.codegen.model.SerializedATN.<init>(SerializedATN.java:46)
at org.antlr.v4.codegen.model.Recognizer.<init>(Recognizer.java:87)
at org.antlr.v4.codegen.model.Lexer.<init>(Lexer.java:51)
at org.antlr.v4.codegen.OutputModelController.lexer(OutputModelController.java:176)
at org.antlr.v4.codegen.OutputModelController.buildLexerOutputModel(OutputModelController.java:129)
at org.antlr.v4.codegen.CodeGenerator.generateLexer(CodeGenerator.java:144)
at org.antlr.v4.codegen.CodeGenPipeline.process(CodeGenPipeline.java:73)
at org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:429)
at org.antlr.v4.Tool.process(Tool.java:379)
at org.antlr.v4.Tool.processGrammarsOnCommandLine(Tool.java:346)
at org.antlr.v4.Tool.main(Tool.java:193)
at com.dicp.fdsl.antlr.FDSLCompiler.main(FDSLCompiler.java:13)
What does this error mean?

I had a similar problem, so I began commenting out lines in my grammar until the error went away.
In my case, the problem was a typo in my rules for numeric literals:
fragment DIGIT : '0'..'9 ' ;
Changing that to either of the following made the error go away:
fragment DIGIT : '0'..'9' ;
fragment DIGIT : [0123456789] ;
It seems likely that the character range from '0' to '9 ' is interpreted as Unicode values, and it produces an unexpectedly large range. This is consistent with the discussion in the ANTLR group at https://github.com/antlr/antlr4/issues/840.

Related

Invalid logback pattern

I was using this working pattern (logback.groovy):
{'((?:password(=|:|>))|(?:secret(=|:))|(?:salt(=|:)))','\$1*******\$3'}
to mask sensitive data. One day I needed to surround it with double quotes, like
was: password=smth
became: "password"="smth"
So I turned regexp into this (just added \" before and after keywords, and also I've tried \\"):
{'(\"?(?:password\"?(=|:|>))|(?:secret\"?(=|:))|(?:salt\"?(=|:)))','\$1*******\$3'}
But I get this error on app startup:
Failed to parse pattern
Unexpected character ('?' (code 63)): was expecting comma to separate Object entries
Can someone please explain to me what am I doing wrong?
If someone wondering here is correct version:
{'(\\\"?(?:password\\\"?(=|:|>))|(?:secret\\\"?(=|:))|(?:salt\\\"?(=|:)))','\$1*******\$3'}

Why Backslash character not removed even after using replace method

I have made a rest request which is returning me a Set in JSON format which is "[\"TestBack\"]".
If I directly parse it in Apex using
Set<String> rw = (Set<String>)JSON.deserialize(response.getBody(),Set<String>.class);
then I get following error
FATAL_ERROR System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
but if I explicitly remove double quote sign by using
Set<String> rw = (Set<String>)JSON.deserialize(response.getBody().substringAfter('"').substringBeforeLast('"'),Set<String>.class);
I got following error
FATAL_ERROR System.JSONException: Unexpected character ('\' (code 92)): expected a valid value
and if I try to use replaceAll method
Set<String> rw = (Set<String>)JSON.deserialize(response.getBody().substringAfter('"').substringBeforeLast('"').replaceAll('\\',''),Set<String>.class);
it shows following error
FATAL_ERROR System.StringException: Invalid regex: Unexpected internal error near index 1
Is there any way to get parse back into Set?

Unable to define max length for ESAPI SafeString Type

I'm trying to use ESAPI in my existing project.
I'm getting an error while trying to use ESAPI.validator().getValidInput() method for SafeString type.
The following is an auto defined Regex, contained in the validation.properties file:
Validator.SafeString=^[.\\p{Alnum}\\p{Space}]{0,1024}$
I assumed that the max length is 1024.
This is my code:
ESAPI.validator().getValidInput("Validationofinput",_appendToSelect,"SafeString",1024, true)
However, I received the following error:
WARN IntrusionDetector - [SECURITY FAILURE Anonymous:null#unknown ->
/ExampleApplication/IntrusionDetector] Invalid input:
context=Validationofinput,
type(SafeString)=^[.\p{Alnum}\p{Space}]{0,1024}$, input=and
ProductCategory like '%test_%'
org.owasp.esapi.errors.ValidationException: Validationofinput: Invalid
input. Please conform to regex ^[.\p{Alnum}\p{Space}]{0,1024}$ with a
maximum length of 1024 at
org.owasp.esapi.reference.validation.StringValidationRule.checkWhitelist(StringValidationRule.java:144)
at
org.owasp.esapi.reference.validation.StringValidationRule.checkWhitelist(StringValidationRule.java:160)
at
org.owasp.esapi.reference.validation.StringValidationRule.getValid(StringValidationRule.java:284)
at
org.owasp.esapi.reference.DefaultValidator.getValidInput(DefaultValidator.java:214)
at
org.owasp.esapi.reference.DefaultValidator.getValidInput(DefaultValidator.java:185)
Can someone tell me what I did wrong?
Okay,
This is failing because your input doesn't match the regular expression, not because of a length violation.
The Posix standard specifies that \p{Alnum} corresponds to the regex [a-zA-Z0-9] and \p{Space} corresponds to \s. In your case, your input will fail because of the characters: ' and % and _
^[.\\p{Alnum}\\p{Space}]{0,1024}$:
^ == At the beginning of the line
[ == Start a character class, match any of the following characters in any order
. == Match an ASCII period
\\p{Alnum} == Match any ASCII alphabet character or number, upper or lower case.
\\p{Space} == Match any ASCII whitespace
] == End character class
{0,1024} == Match any number of characters from 0 to 1024
$ == All the way until the end of the line.

How to create a lexical analyzer in ANTLR 4 that can catch different types of lexical errors

I am using ANTLR 4 to create my lexer, but I don't how to create a lexical analyzer that catches different types of lexical errors.
For example:
If I have an unrecognized symbol like ^ the lexical analyzer should a report an error like this "Unrecognized symbol "^" "
If I have an invalid identifier like 2n the lexical analyzer should report an error like this "identifier "2n" must begin with a letter"
Please can you help me.
Create an error token rule for each known error and an "catchall" error token rule at the end like this:
// valid tokens first!
Number : [0-9]+;
Identifier : [a-zA-Z] [a-zA-Z0-9]*;
//...
// "error" tokens
// don't use these tokens in your grammar; They will show up as extraneous tokens during parsing and can be handled if desired.
InvalidIdentifier : [0-9]([0-9a-zA-Z])+;
ACommonInvalidToken : '^'; // if you want to be more specific for certain cases
// add more to address common mistakes
UnknownToken : . ; // the "catch-all" error token; be sure not to be too greedy...

Java Regular Expression Issue

The following exception is thrown when this [0-9]+_[0-9]+_[0-9]+(_[0-9]+){0,1} regex is used.
Caused by: java.util.regex.PatternSyntaxException: Unclosed counted closure near index 31
[0-9]+_[0-9]+_[0-9]+(_[0-9]+){0
^
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.closure(Pattern.java:2759)
at java.util.regex.Pattern.group0(Pattern.java:2537)
at java.util.regex.Pattern.sequence(Pattern.java:1806)
at java.util.regex.Pattern.expr(Pattern.java:1752)
at java.util.regex.Pattern.compile(Pattern.java:1460)
at java.util.regex.Pattern.<init>(Pattern.java:1133)
at java.util.regex.Pattern.compile(Pattern.java:823)
I am reading this from a XML File.
You probably want this:
[0-9]+_[0-9]+_[0-9]+(_[0-9]+)?
You have to escape commas with backslash:
[0-9]+_[0-9]+_[0-9]+(_[0-9]+){0\,1}

Categories