Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape bad words during grammar generation #3451

Merged
merged 3 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[notes]
https://github.com/antlr/antlr4/issues/1070

[type]
Lexer

[grammar]
lexer grammar L;

channels { break }

A: 'a' -> mode(for);

mode for;
B: 'b' -> channel(break);

[input]
ab

[output]
[@0,0:0='a',<1>,1:0]
[@1,1:1='b',<2>,channel=2,1:1]
[@2,2:1='<EOF>',<-1>,1:2]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[notes]
https://github.com/antlr/antlr4/issues/1070

[type]
Parser

[grammar]
grammar G;

root
: {0==0}? continue+ {<writeln("$text")>}
;

continue returns [int return]
parrt marked this conversation as resolved.
Show resolved Hide resolved
: for for? {1==1}? #else
| break=BREAK BREAK+ (for | IF) #else
| if+=IF if+=IF* #int
| continue CONTINUE_ {<AssignLocal("$return","0")>} #class
;

args[int else] locals [int return]
: for
;

for: FOR;
FOR: 'for ';
BREAK: 'break ';
IF: 'if ';
CONTINUE_: 'continue';

[start]
root

[input]
for for break break for if if for continue

[output]
"""for for break break for if if for continue
"""
212 changes: 65 additions & 147 deletions tool/resources/org/antlr/v4/tool/templates/codegen/CSharp/CSharp.stg

Large diffs are not rendered by default.

134 changes: 67 additions & 67 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg

Large diffs are not rendered by default.

74 changes: 37 additions & 37 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Dart/Dart.stg
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ bool _<r.name>_sempred(dynamic _localctx, int predIndex) {

RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= <<

<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else><endif><currentRule.ctxType> <currentRule.name>(<args; separator=", ">) {
dynamic _localctx = <currentRule.ctxType>(context, state<currentRule.args:{a | , <a.name>}>);
<if(currentRule.modifiers)><currentRule.modifiers:{f | <f> }><else><endif><currentRule.ctxType> <currentRule.escapedName>(<args; separator=", ">) {
dynamic _localctx = <currentRule.ctxType>(context, state<currentRule.args:{a | , <a.escapedName>}>);
enterRule(_localctx, <currentRule.startState>, RULE_<currentRule.name>);
<namedActions.init>
<locals; separator="\n">
Expand All @@ -345,10 +345,10 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,
namedActions,finallyAction,postamble) ::=
<<

<currentRule.ctxType> <currentRule.name>([int _p = 0]<args:{a | , <a>}>) {
<currentRule.ctxType> <currentRule.escapedName>([int _p = 0]<args:{a | , <a>}>) {
final _parentctx = context;
final _parentState = state;
dynamic _localctx = <currentRule.ctxType>(context, _parentState<currentRule.args:{a | , <a.name>}>);
dynamic _localctx = <currentRule.ctxType>(context, _parentState<currentRule.args:{a | , <a.escapedName>}>);
var _prevctx = _localctx;
var _startState = <currentRule.startState>;
enterRecursionRule(_localctx, <currentRule.startState>, RULE_<currentRule.name>, _p);
Expand Down Expand Up @@ -543,7 +543,7 @@ cases(ttypes) ::= <<

InvokeRule(r, argExprsChunks) ::=<<
state = <r.stateNumber>;
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif><r.name>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>);
<if(r.labels)><r.labels:{l | <labelref(l)> = }><endif><r.escapedName>(<if(r.ast.options.p)><r.ast.options.p><if(argExprsChunks)>,<endif><endif><argExprsChunks>);
>>

MatchToken(m) ::= <<
Expand Down Expand Up @@ -605,15 +605,15 @@ LexerPushModeCommand(arg, grammar) ::= "pushMode(<arg>);"

ActionText(t) ::= "<t.text>"
ActionTemplate(t) ::= "<t.st>"
ArgRef(a) ::= "_localctx.<a.name>"
LocalRef(a) ::= "_localctx.<a.name>"
RetValueRef(a) ::= "_localctx.<a.name>"
QRetValueRef(a) ::= "<ctx(a)>.<a.dict>.<a.name>"
ArgRef(a) ::= "_localctx.<a.escapedName>"
LocalRef(a) ::= "_localctx.<a.escapedName>"
RetValueRef(a) ::= "_localctx.<a.escapedName>"
QRetValueRef(a) ::= "<ctx(a)>.<a.dict>.<a.escapedName>"
/** How to translate $tokenLabel */
TokenRef(t) ::= "<ctx(t)>.<t.name>"
LabelRef(t) ::= "<ctx(t)>.<t.name>"
ListLabelRef(t) ::= "<ctx(t)>.<ListLabelName(t.name)>"
SetAttr(s,rhsChunks) ::= "<ctx(s)>.<s.name> = <rhsChunks>;"
TokenRef(t) ::= "<ctx(t)>.<t.escapedName>"
LabelRef(t) ::= "<ctx(t)>.<t.escapedName>"
ListLabelRef(t) ::= "<ctx(t)>.<ListLabelName(t.escapedName)>"
SetAttr(s,rhsChunks) ::= "<ctx(s)>.<s.escapedName> = <rhsChunks>;"

TokenLabelType() ::= "<file.TokenLabelType; null={Token}>"
InputSymbolType() ::= "<file.InputSymbolType; null={Token}>"
Expand All @@ -638,35 +638,35 @@ ThisRulePropertyRef_text(r) ::= "tokenStream.getTextRange(_localctx.start, toke
ThisRulePropertyRef_ctx(r) ::= "_localctx"
ThisRulePropertyRef_parser(r) ::= "this"

NonLocalAttrRef(s) ::= "(getInvokingContext(<s.ruleIndex>) as <s.ruleName; format=\"cap\">Context).<s.name>"
NonLocalAttrRef(s) ::= "(getInvokingContext(<s.ruleIndex>) as <s.ruleName; format=\"cap\">Context).<s.escapedName>"
SetNonLocalAttr(s, rhsChunks) ::=
"(getInvokingContext(<s.ruleIndex>) as <s.ruleName; format=\"cap\">Context).<s.name> = <rhsChunks>;"
"(getInvokingContext(<s.ruleIndex>) as <s.ruleName; format=\"cap\">Context).<s.escapedName> = <rhsChunks>;"

AddToLabelList(a) ::= "<ctx(a.label)>.<a.listName>.add(<labelref(a.label)>);"

TokenDecl(t) ::= "<TokenLabelType()>? <t.name>"
TokenTypeDecl(t) ::= "int <t.name>;"
TokenListDecl(t) ::= "var <t.name> = \<Token>[]"
RuleContextDecl(r) ::= "<r.ctxName>? <r.name>"
RuleContextListDecl(rdecl) ::= "var <rdecl.name> = \<<rdecl.ctxName>>[]"
TokenDecl(t) ::= "<TokenLabelType()>? <t.escapedName>"
TokenTypeDecl(t) ::= "int <t.escapedName>;"
TokenListDecl(t) ::= "var <t.escapedName> = \<Token>[]"
RuleContextDecl(r) ::= "<r.ctxName>? <r.escapedName>"
RuleContextListDecl(rdecl) ::= "var <rdecl.escapedName> = \<<rdecl.ctxName>>[]"

ContextTokenGetterDecl(t) ::= <<
TerminalNode? <t.name>() => getToken(<parser.name>.TOKEN_<t.name>, 0);
TerminalNode? <t.escapedName>() => getToken(<parser.name>.TOKEN_<t.name>, 0);
>>
ContextTokenListGetterDecl(t) ::= <<
List\<TerminalNode> <t.name>s() => getTokens(<parser.name>.TOKEN_<t.name>);
>>
ContextTokenListIndexedGetterDecl(t) ::= <<
TerminalNode? <t.name>(int i) => getToken(<parser.name>.TOKEN_<t.name>, i);
TerminalNode? <t.escapedName>(int i) => getToken(<parser.name>.TOKEN_<t.name>, i);
>>
ContextRuleGetterDecl(r) ::= <<
<r.ctxName>? <r.name>() => getRuleContext\<<r.ctxName>\>(0);
<r.ctxName>? <r.escapedName>() => getRuleContext\<<r.ctxName>\>(0);
>>
ContextRuleListGetterDecl(r) ::= <<
List\<<r.ctxName>\> <r.name>s() => getRuleContexts\<<r.ctxName>\>();
>>
ContextRuleListIndexedGetterDecl(r) ::= <<
<r.ctxName>? <r.name>(int i) => getRuleContext\<<r.ctxName>\>(i);
<r.ctxName>? <r.escapedName>(int i) => getRuleContext\<<r.ctxName>\>(i);
>>

LexerRuleContext() ::= "RuleContext"
Expand All @@ -685,11 +685,11 @@ CaptureNextTokenType(d) ::= "<d.varName> = tokenStream.LA(1)!;"

StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers)
::= <<
class <struct.name> extends <if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif><if(interfaces)> implements <interfaces; separator=", "><endif> {
class <struct.escapedName> extends <if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif><if(interfaces)> implements <interfaces; separator=", "><endif> {
<attrs:{a | <a>;}; separator="\n">
<getters:{g | <g>}; separator="\n">
<struct.name>([ParserRuleContext? parent, int? invokingState<ctorAttrs:{a | , <a>}>]) : super(parent, invokingState)<if(struct.ctorAttrs)> {
<struct.ctorAttrs:{a | this.<a.name> = <a.name>;}; separator="\n">
<struct.escapedName>([ParserRuleContext? parent, int? invokingState<ctorAttrs:{a | , <a>}>]) : super(parent, invokingState)<if(struct.ctorAttrs)> {
<struct.ctorAttrs:{a | this.<a.escapedName> = <a.escapedName>;}; separator="\n">
}<else>;<endif>

@override
Expand All @@ -698,7 +698,7 @@ class <struct.name> extends <if(contextSuperClass)><contextSuperClass><else>Pars
@override
void copyFrom(<if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif> ctx) {
super.copyFrom(ctx);
<struct.attrs:{a | if((ctx as <struct.name>).<a.name> != null) this.<a.name> = (ctx as <struct.name>).<a.name>;}; separator="\n">
<struct.attrs:{a | if((ctx as <struct.escapedName>).<a.escapedName> != null) this.<a.escapedName> = (ctx as <struct.escapedName>).<a.escapedName>;}; separator="\n">
}
<endif>
<dispatchMethods; separator="\n">
Expand All @@ -707,10 +707,10 @@ class <struct.name> extends <if(contextSuperClass)><contextSuperClass><else>Pars
>>

AltLabelStructDecl(struct,attrs,getters,dispatchMethods) ::= <<
class <struct.name> extends <struct.parentRule; format="cap">Context {
class <struct.escapedName> extends <struct.parentRule; format="cap">Context {
<attrs:{a | <a>;}; separator="\n">
<getters:{g | <g>}; separator="\n">
<struct.name>(<struct.parentRule; format="cap">Context ctx) { copyFrom(ctx); }
<struct.escapedName>(<struct.parentRule; format="cap">Context ctx) { copyFrom(ctx); }
<dispatchMethods; separator="\n">
}
>>
Expand All @@ -733,10 +733,10 @@ T? accept\<T>(ParseTreeVisitor\<T> visitor) {
}
>>

AttributeDecl(d) ::= "<d.type><if(d.initValue)><else>?<endif> <d.name><if(d.initValue)> = <d.initValue><endif>"
AttributeDecl(d) ::= "<d.type><if(d.initValue)><else>?<endif> <d.escapedName><if(d.initValue)> = <d.initValue><endif>"

// If we don't know location of label def x, use this template
labelref(x) ::= "<if(!x.isLocal)>_localctx.<endif><x.name>"
labelref(x) ::= "<if(!x.isLocal)>_localctx.<endif><x.escapedName>"

// For any action chunk, what is correctly-typed context struct ptr?
ctx(actionChunk) ::= "_localctx"
Expand Down Expand Up @@ -806,18 +806,18 @@ class <lexer.name> extends <superClass; null="Lexer"> {
static const int
<lexer.tokens:{k | TOKEN_<k> = <lexer.tokens.(k)>}; separator=", ", wrap, anchor>;
<endif>
<if(lexer.channels)>
<if(lexer.escapedChannels)>
static const int
<lexer.channels:{c | <c> = <lexer.channels.(c)>}; separator=", ", wrap, anchor>;
<lexer.escapedChannels:{c | <c> = <lexer.escapedChannels.(c)>}; separator=", ", wrap, anchor>;
<endif>
<if(rest(lexer.modes))>
<if(rest(lexer.escapedModeNames))>
static const int
<rest(lexer.modes):{m | <m> = <i>}; separator=", ", wrap, anchor>;
<rest(lexer.escapedModeNames):{m | <m> = <i>}; separator=", ", wrap, anchor>;
<endif>

@override
final List\<String> channelNames = [
'DEFAULT_TOKEN_CHANNEL', 'HIDDEN'<if (lexer.channels)>, <lexer.channels:{c| '<c>'}; separator=", ", wrap, anchor><endif>
'DEFAULT_TOKEN_CHANNEL', 'HIDDEN'<if (lexer.channelNames)>, <lexer.channelNames:{c| '<c>'}; separator=", ", wrap, anchor><endif>
];

@override
Expand Down
Loading