diff --git a/pony.g b/pony.g index 6b2d708ae7..7c6780822a 100644 --- a/pony.g +++ b/pony.g @@ -22,7 +22,7 @@ use_ffi ; class_def - : ('type' | 'interface' | 'trait' | 'primitive' | 'struct' | 'class' | 'actor') ('\' ID (',' ID)* '\')? '@'? cap? ID typeparams? ('is' type)? STRING? members + : ('type' | 'interface' | 'trait' | 'primitive' | 'struct' | 'class' | 'actor') ('\\' ID (',' ID)* '\\')? '@'? cap? ID typeparams? ('is' type)? STRING? members ; members @@ -34,11 +34,11 @@ field ; method - : ('fun' | 'be' | 'new') ('\' ID (',' ID)* '\')? cap? ID typeparams? ('(' | LPAREN_NEW) params? ')' (':' type)? '?'? STRING? ('if' rawseq)? ('=>' rawseq)? + : ('fun' | 'be' | 'new') ('\\' ID (',' ID)* '\\')? cap? ID typeparams? ('(' | LPAREN_NEW) params? ')' (':' type)? '?'? STRING? ('if' rawseq)? ('=>' rawseq)? ; annotatedrawseq - : ('\' ID (',' ID)* '\')? (exprseq | jump) + : ('\\' ID (',' ID)* '\\')? (exprseq | jump) ; rawseq @@ -88,30 +88,30 @@ binop ; nextterm - : 'if' ('\' ID (',' ID)* '\')? rawseq 'then' rawseq (elseif | ('else' annotatedrawseq))? 'end' - | 'ifdef' ('\' ID (',' ID)* '\')? infix 'then' rawseq (elseifdef | ('else' annotatedrawseq))? 'end' - | 'match' ('\' ID (',' ID)* '\')? rawseq caseexpr* ('else' annotatedrawseq)? 'end' - | 'while' ('\' ID (',' ID)* '\')? rawseq 'do' rawseq ('else' annotatedrawseq)? 'end' - | 'repeat' ('\' ID (',' ID)* '\')? rawseq 'until' annotatedrawseq ('else' annotatedrawseq)? 'end' - | 'for' ('\' ID (',' ID)* '\')? idseq 'in' rawseq 'do' rawseq ('else' annotatedrawseq)? 'end' - | 'with' ('\' ID (',' ID)* '\')? (withelem (',' withelem)*) 'do' rawseq ('else' annotatedrawseq)? 'end' - | 'try' ('\' ID (',' ID)* '\')? rawseq ('else' annotatedrawseq)? ('then' annotatedrawseq)? 'end' - | 'recover' ('\' ID (',' ID)* '\')? cap? rawseq 'end' + : 'if' ('\\' ID (',' ID)* '\\')? rawseq 'then' rawseq (elseif | ('else' annotatedrawseq))? 'end' + | 'ifdef' ('\\' ID (',' ID)* '\\')? infix 'then' rawseq (elseifdef | ('else' annotatedrawseq))? 'end' + | 'match' ('\\' ID (',' ID)* '\\')? rawseq caseexpr* ('else' annotatedrawseq)? 'end' + | 'while' ('\\' ID (',' ID)* '\\')? rawseq 'do' rawseq ('else' annotatedrawseq)? 'end' + | 'repeat' ('\\' ID (',' ID)* '\\')? rawseq 'until' annotatedrawseq ('else' annotatedrawseq)? 'end' + | 'for' ('\\' ID (',' ID)* '\\')? idseq 'in' rawseq 'do' rawseq ('else' annotatedrawseq)? 'end' + | 'with' ('\\' ID (',' ID)* '\\')? (withelem (',' withelem)*) 'do' rawseq ('else' annotatedrawseq)? 'end' + | 'try' ('\\' ID (',' ID)* '\\')? rawseq ('else' annotatedrawseq)? ('then' annotatedrawseq)? 'end' + | 'recover' ('\\' ID (',' ID)* '\\')? cap? rawseq 'end' | 'consume' cap? term | nextpattern | '#' postfix ; term - : 'if' ('\' ID (',' ID)* '\')? rawseq 'then' rawseq (elseif | ('else' annotatedrawseq))? 'end' - | 'ifdef' ('\' ID (',' ID)* '\')? infix 'then' rawseq (elseifdef | ('else' annotatedrawseq))? 'end' - | 'match' ('\' ID (',' ID)* '\')? rawseq caseexpr* ('else' annotatedrawseq)? 'end' - | 'while' ('\' ID (',' ID)* '\')? rawseq 'do' rawseq ('else' annotatedrawseq)? 'end' - | 'repeat' ('\' ID (',' ID)* '\')? rawseq 'until' annotatedrawseq ('else' annotatedrawseq)? 'end' - | 'for' ('\' ID (',' ID)* '\')? idseq 'in' rawseq 'do' rawseq ('else' annotatedrawseq)? 'end' - | 'with' ('\' ID (',' ID)* '\')? (withelem (',' withelem)*) 'do' rawseq ('else' annotatedrawseq)? 'end' - | 'try' ('\' ID (',' ID)* '\')? rawseq ('else' annotatedrawseq)? ('then' annotatedrawseq)? 'end' - | 'recover' ('\' ID (',' ID)* '\')? cap? rawseq 'end' + : 'if' ('\\' ID (',' ID)* '\\')? rawseq 'then' rawseq (elseif | ('else' annotatedrawseq))? 'end' + | 'ifdef' ('\\' ID (',' ID)* '\\')? infix 'then' rawseq (elseifdef | ('else' annotatedrawseq))? 'end' + | 'match' ('\\' ID (',' ID)* '\\')? rawseq caseexpr* ('else' annotatedrawseq)? 'end' + | 'while' ('\\' ID (',' ID)* '\\')? rawseq 'do' rawseq ('else' annotatedrawseq)? 'end' + | 'repeat' ('\\' ID (',' ID)* '\\')? rawseq 'until' annotatedrawseq ('else' annotatedrawseq)? 'end' + | 'for' ('\\' ID (',' ID)* '\\')? idseq 'in' rawseq 'do' rawseq ('else' annotatedrawseq)? 'end' + | 'with' ('\\' ID (',' ID)* '\\')? (withelem (',' withelem)*) 'do' rawseq ('else' annotatedrawseq)? 'end' + | 'try' ('\\' ID (',' ID)* '\\')? rawseq ('else' annotatedrawseq)? ('then' annotatedrawseq)? 'end' + | 'recover' ('\\' ID (',' ID)* '\\')? cap? rawseq 'end' | 'consume' cap? term | pattern | '#' postfix @@ -122,15 +122,15 @@ withelem ; caseexpr - : '|' ('\' ID (',' ID)* '\')? pattern? ('if' rawseq)? ('=>' rawseq)? + : '|' ('\\' ID (',' ID)* '\\')? pattern? ('if' rawseq)? ('=>' rawseq)? ; elseifdef - : 'elseif' ('\' ID (',' ID)* '\')? infix 'then' rawseq (elseifdef | ('else' annotatedrawseq))? + : 'elseif' ('\\' ID (',' ID)* '\\')? infix 'then' rawseq (elseifdef | ('else' annotatedrawseq))? ; elseif - : 'elseif' ('\' ID (',' ID)* '\')? rawseq 'then' rawseq (elseif | ('else' annotatedrawseq))? + : 'elseif' ('\\' ID (',' ID)* '\\')? rawseq 'then' rawseq (elseif | ('else' annotatedrawseq))? ; idseq @@ -193,9 +193,9 @@ nextatom | literal | LPAREN_NEW rawseq tuple? ')' | LSQUARE_NEW ('as' type ':')? rawseq (',' rawseq)* ']' - | 'object' ('\' ID (',' ID)* '\')? cap? ('is' type)? members 'end' - | '{' ('\' ID (',' ID)* '\')? cap? ID? typeparams? ('(' | LPAREN_NEW) params? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq '}' cap? - | 'lambda' ('\' ID (',' ID)* '\')? cap? ID? typeparams? ('(' | LPAREN_NEW) params? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq 'end' + | 'object' ('\\' ID (',' ID)* '\\')? cap? ('is' type)? members 'end' + | '{' ('\\' ID (',' ID)* '\\')? cap? ID? typeparams? ('(' | LPAREN_NEW) params? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq '}' cap? + | 'lambda' ('\\' ID (',' ID)* '\\')? cap? ID? typeparams? ('(' | LPAREN_NEW) params? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq 'end' | '@' (ID | STRING) typeargs? ('(' | LPAREN_NEW) positional? named? ')' '?'? | '__loc' ; @@ -206,9 +206,9 @@ atom | literal | ('(' | LPAREN_NEW) rawseq tuple? ')' | ('[' | LSQUARE_NEW) ('as' type ':')? rawseq (',' rawseq)* ']' - | 'object' ('\' ID (',' ID)* '\')? cap? ('is' type)? members 'end' - | '{' ('\' ID (',' ID)* '\')? cap? ID? typeparams? ('(' | LPAREN_NEW) params? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq '}' cap? - | 'lambda' ('\' ID (',' ID)* '\')? cap? ID? typeparams? ('(' | LPAREN_NEW) params? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq 'end' + | 'object' ('\\' ID (',' ID)* '\\')? cap? ('is' type)? members 'end' + | '{' ('\\' ID (',' ID)* '\\')? cap? ID? typeparams? ('(' | LPAREN_NEW) params? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq '}' cap? + | 'lambda' ('\\' ID (',' ID)* '\\')? cap? ID? typeparams? ('(' | LPAREN_NEW) params? ')' lambdacaptures? (':' type)? '?'? '=>' rawseq 'end' | '@' (ID | STRING) typeargs? ('(' | LPAREN_NEW) positional? named? ')' '?'? | '__loc' ; diff --git a/src/libponyc/ast/bnfprint.c b/src/libponyc/ast/bnfprint.c index de85daadbf..b833bf0bcd 100644 --- a/src/libponyc/ast/bnfprint.c +++ b/src/libponyc/ast/bnfprint.c @@ -250,6 +250,27 @@ static bnf_t* bnf_add(bnf_t* bnf, bnf_t* parent) return bnf; } +// Print out a quoted token, in ANTLR syntax. +// Certain characters should be escaped before printing. +static void bnf_print_quoted_token(const char* token) +{ + printf("'"); + for(const char* c = token; *c != '\0'; c++) { + // See the definition of LITERAL_CHAR and ESC in + // http://www.antlr3.org/grammar/ANTLR/ANTLRv3.g + switch(*c) { + case '\'': printf("\\'"); break; + case '\\': printf("\\\\"); break; + case '\n': printf("\\n"); break; + case '\r': printf("\\r"); break; + case '\t': printf("\\t"); break; + case '\b': printf("\\b"); break; + case '\f': printf("\\f"); break; + default: putchar(*c); break; + } + } + printf("'"); +} // Print out the given node, in ANTLR syntax. // The top_format parameter indicates we should use top level node within rule @@ -308,7 +329,7 @@ static void bnf_print(bnf_t* bnf, bool top_format) break; case BNF_QUOTED_TOKEN: - printf("'%s'", bnf->name); + bnf_print_quoted_token(bnf->name); break; case BNF_NEVER: