Permalink
Browse files

version 1.8.9: fixed a bug when literals containing "#{" were not tra…

…nslated with escapes, causing syntax errors in the output
  • Loading branch information...
Kyle Yetter
Kyle Yetter committed Jan 29, 2011
1 parent 140b6f7 commit a9826ee85c77083c65fd3f9be4707f334142af53
View
@@ -3,7 +3,7 @@
# project meta-data used for various project tasks like building gem packages
#
name: antlr3
-version: "1.8.8"
+version: "1.8.9"
antlr_version: "3.2.1"
summary: |-
View
@@ -15,45 +15,46 @@ Hoe.spec "antlr3" do
)
self.test_globs = [
+ "test/unit/test-streams.rb",
+ "test/unit/test-template.rb",
+ "test/unit/test-unicode-stream.rb",
"test/unit/test-dfa.rb",
+ "test/unit/test-exceptions.rb",
+ "test/unit/test-scheme.rb",
"test/unit/test-tree-wizard.rb",
- "test/unit/test-streams.rb",
- "test/unit/test-recognizers.rb",
"test/unit/test-trees.rb",
- "test/unit/test-exceptions.rb",
+ "test/unit/test-recognizers.rb",
"test/unit/test-scope.rb",
- "test/unit/test-scheme.rb",
- "test/unit/test-template.rb",
- "test/functional/delegation/import.rb",
- "test/functional/lexer/xml.rb",
+ "test/functional/main/main-scripts.rb",
+ "test/functional/template-output/template-output.rb",
+ "test/functional/tree-parser/basic.rb",
"test/functional/lexer/properties.rb",
- "test/functional/lexer/nuances.rb",
- "test/functional/lexer/basic.rb",
"test/functional/lexer/filter-mode.rb",
"test/functional/lexer/syn-pred.rb",
- "test/functional/debugging/rule-tracing.rb",
- "test/functional/debugging/profile-mode.rb",
- "test/functional/debugging/debug-mode.rb",
- "test/functional/ast-output/hetero-nodes.rb",
- "test/functional/ast-output/construction.rb",
- "test/functional/ast-output/rewrites.rb",
- "test/functional/ast-output/tree-rewrite.rb",
- "test/functional/ast-output/auto-ast.rb",
- "test/functional/main/main-scripts.rb",
- "test/functional/tree-parser/basic.rb",
+ "test/functional/lexer/basic.rb",
+ "test/functional/lexer/nuances.rb",
+ "test/functional/lexer/xml.rb",
+ "test/functional/delegation/import.rb",
+ "test/functional/parser/scopes.rb",
+ "test/functional/parser/properties.rb",
+ "test/functional/parser/basic.rb",
"test/functional/parser/predicates.rb",
+ "test/functional/parser/ll-star.rb",
"test/functional/parser/backtracking.rb",
- "test/functional/parser/properties.rb",
+ "test/functional/parser/calc.rb",
"test/functional/parser/nuances.rb",
- "test/functional/parser/rule-methods.rb",
- "test/functional/parser/basic.rb",
"test/functional/parser/actions.rb",
- "test/functional/parser/calc.rb",
- "test/functional/parser/ll-star.rb",
- "test/functional/parser/scopes.rb",
- "test/functional/template-output/template-output.rb",
+ "test/functional/parser/rule-methods.rb",
+ "test/functional/debugging/debug-mode.rb",
+ "test/functional/debugging/profile-mode.rb",
+ "test/functional/debugging/rule-tracing.rb",
+ "test/functional/token-rewrite/basic.rb",
"test/functional/token-rewrite/via-parser.rb",
- "test/functional/token-rewrite/basic.rb"
+ "test/functional/ast-output/tree-rewrite.rb",
+ "test/functional/ast-output/construction.rb",
+ "test/functional/ast-output/rewrites.rb",
+ "test/functional/ast-output/hetero-nodes.rb",
+ "test/functional/ast-output/auto-ast.rb"
]
end
View
@@ -477,4 +477,135 @@ public String encodeIntAsCharEscape( final int v ) {
return String.valueOf( intValue );
}
+
+ /** Convert from an ANTLR string literal found in a grammar file to
+ * an equivalent string literal in the target language. For Java, this
+ * is the translation 'a\n"' -> "a\n\"". Expect single quotes
+ * around the incoming literal. Just flip the quotes and replace
+ * double quotes with \"
+ *
+ * Note that we have decided to allow poeple to use '\"' without
+ * penalty, so we must build the target string in a loop as Utils.replae
+ * cannot handle both \" and " without a lot of messing around.
+ *
+ */
+ public String getTargetStringLiteralFromANTLRStringLiteral(
+ CodeGenerator generator,
+ String literal)
+ {
+ StringBuilder sb = new StringBuilder();
+ StringBuffer is = new StringBuffer(literal);
+ int lastIndex = is.length() - 1;
+
+ // Opening quote
+ //
+ sb.append('"');
+
+ for ( int i = 1; i < lastIndex; i++ ) {
+ if (is.charAt(i) == '\\') {
+ // Anything escaped is what it is! We assume that
+ // people know how to escape characters correctly. However
+ // we catch anything that does not need an escape in Java (which
+ // is what the default implementation is dealing with and remove
+ // the escape. The C target does this for instance.
+ //
+ switch (is.charAt(i+1)) {
+ // Pass through any escapes that Java also needs
+ //
+ case '"':
+ case 'n':
+ case 'r':
+ case 't':
+ case 'b':
+ case 'f':
+ case '\\':
+ case 'u': // Assume unnnn
+ sb.append('\\'); // Pass the escape through
+ break;
+ default:
+ // Remove the escape by virtue of not adding it here
+ // Thus \' becomes ' and so on
+ //
+ break;
+ }
+
+ // Go past the \ character
+ //
+ i++;
+ } else {
+
+ // in ruby, double quoted strings have interpolation signifiers that
+ // will cause a syntax error in the output if they are not properly
+ // escaped
+
+ if ( is.charAt( i ) == '#' && i < lastIndex - 1 ) {
+ switch ( is.charAt( i + 1 ) ) {
+ case '{':
+ case '@':
+ case '$':
+ sb.append('\\');
+ break;
+ default:
+ break;
+ // pass through
+ }
+ } else if (is.charAt(i) == '"') {
+ sb.append('\\');
+ }
+
+ }
+ // Add in the next character, which may have been escaped
+ //
+ sb.append(is.charAt(i));
+ }
+
+ // Append closing " and return
+ //
+ sb.append('"');
+
+ return sb.toString();
+ }
+
+
+ public String getTargetStringLiteralFromString(String s, boolean quoted) {
+ if ( s == null ) { return null; }
+
+ StringBuffer buf = new StringBuffer();
+
+ if ( quoted ) { buf.append('"'); }
+
+ for ( int i = 0; i < s.length(); i++ ) {
+ int c = s.charAt( i );
+
+ if ( c != '\'' && // don't escape single quotes in strings for java
+ c < targetCharValueEscape.length &&
+ targetCharValueEscape[c] != null )
+ {
+ buf.append(targetCharValueEscape[c]);
+ }
+ else if ( c == '#' && i < s.length() - 1 )
+ {
+ switch ( s.charAt( i + 1 ) ) {
+ case '{':
+ case '@':
+ case '$':
+ buf.append( "\\#" );
+ break;
+ default:
+ buf.append( '#' );
+ break;
+ }
+ }
+ else
+ {
+ buf.append((char)c);
+ }
+ }
+ if ( quoted ) {
+ buf.append('"');
+ }
+ return buf.toString();
+ }
+
+
}
View
Binary file not shown.
View
@@ -20,7 +20,7 @@ module ANTLR3
#
MAJOR_VERSION = 1
MINOR_VERSION = 8
- PATCH_VERSION = 8
+ PATCH_VERSION = 9
VERSION = [ MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION ]
VERSION_STRING = VERSION.join( '.' ).freeze
View
@@ -1459,4 +1459,4 @@ placeAction(scope, name) ::= <<
<endif>
>>
-runtimeLibraryVersion() ::= "1.8.8"
+runtimeLibraryVersion() ::= "1.8.9"
@@ -0,0 +1,52 @@
+grammar SearchExpression;
+
+options {
+ language = Ruby;
+ output = AST;
+}
+
+tokens {
+ MATCH;
+}
+
+
+search_expression
+ : not_expression ( ('and'^ | 'or'^ | 'xor'^) not_expression )*
+ ;
+
+not_expression
+ : ( 'not'^ )? atom
+ ;
+
+atom
+ : group
+ | ( CHUNK ':'^ )? term+
+ ;
+
+group
+ : '('! search_expression ')'!
+ | '['! search_expression ']'!
+ | '{'! search_expression '}'!
+ ;
+
+term
+ : CHUNK
+ | STRING
+ | REGEX
+ ;
+
+CHUNK
+ : ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' | '-' | '.' | ',' | '\\' . )+
+ ;
+
+REGEX
+ : '/' ( '\\' . | ~'/' )+ '/' ('i' | 'm' | 'x')*
+ ;
+
+STRING
+ : '"' ( ~('"'|'\\') | '\\' . )* '"'
+ | '\'' ( ~('\''|'\\') | '\\' . )* '\''
+ ;
+
+WS : (' ' | '\t' | '\n' | '\r' )+ { $channel = HIDDEN }
+ ;
Oops, something went wrong.

0 comments on commit a9826ee

Please sign in to comment.