rm'd $foo references from lexer actions and added a good error message.

This commit is contained in:
Terence Parr 2012-09-23 18:20:04 -07:00
parent bca63c64aa
commit f43cd15dd9
8 changed files with 22 additions and 60 deletions

View File

@ -2,5 +2,5 @@ grammar T;
s : INT { System.out.println($start.getText());} ;
INT : [0-9]+ {$type = 3; String x = $text; $channel, $mode} ;
INT : [0-9]+ {$x.type = 3; String x = $text; $channel, $mode} ;
WS : [ \t\n]+ -> skip ;

View File

@ -522,7 +522,6 @@ 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>;"
LexerSetAttr(s,rhsChunks) ::= "_<s.name> = <rhsChunks>;" // _type etc...
TokenLabelType() ::= "<file.TokenLabelType; null={Token}>"
InputSymbolType() ::= "<file.InputSymbolType; null={Token}>"

View File

@ -36,7 +36,6 @@ import org.antlr.v4.codegen.model.chunk.ActionChunk;
import org.antlr.v4.codegen.model.chunk.ActionText;
import org.antlr.v4.codegen.model.chunk.ArgRef;
import org.antlr.v4.codegen.model.chunk.LabelRef;
import org.antlr.v4.codegen.model.chunk.LexerSetAttr;
import org.antlr.v4.codegen.model.chunk.ListLabelRef;
import org.antlr.v4.codegen.model.chunk.LocalRef;
import org.antlr.v4.codegen.model.chunk.NonLocalAttrRef;
@ -240,7 +239,6 @@ public class ActionTranslator implements ActionSplitterListener {
gen.g.tool.log("action-translator", "setAttr "+x+" "+rhs);
List<ActionChunk> rhsChunks = translateActionChunk(factory,rf,rhs.getText(),node);
SetAttr s = new SetAttr(nodeContext, x.getText(), rhsChunks);
if ( factory.getGrammar().isLexer() ) s = new LexerSetAttr(nodeContext, x.getText(), rhsChunks);
chunks.add(s);
}

View File

@ -1,40 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.codegen.model.chunk;
import org.antlr.v4.codegen.model.decl.StructDecl;
import java.util.List;
public class LexerSetAttr extends SetAttr {
public LexerSetAttr(StructDecl ctx, String name, List<ActionChunk> rhsChunks) {
super(ctx, name, rhsChunks);
}
}

View File

@ -118,6 +118,11 @@ public class AttributeChecks implements ActionSplitterListener {
// $x.y
@Override
public void qualifiedAttr(String expr, Token x, Token y) {
if ( g.isLexer() ) {
errMgr.grammarError(ErrorType.ATTRIBUTE_IN_LEXER_ACTION,
g.fileName, x, x.getText()+"."+y.getText(), expr);
return;
}
if ( node.resolver.resolveToAttribute(x.getText(), node)!=null ) {
// must be a member access to a predefined attribute like $ctx.foo
attr(expr, x);
@ -149,15 +154,25 @@ public class AttributeChecks implements ActionSplitterListener {
@Override
public void setAttr(String expr, Token x, Token rhs) {
if ( g.isLexer() ) {
errMgr.grammarError(ErrorType.ATTRIBUTE_IN_LEXER_ACTION,
g.fileName, x, x.getText(), expr);
return;
}
if ( node.resolver.resolveToAttribute(x.getText(), node)==null ) {
errMgr.grammarError(ErrorType.UNKNOWN_SIMPLE_ATTRIBUTE,
g.fileName, x, x.getText(), expr);
}
new AttributeChecks(g, r, alt, node, rhs).examineAction();
}
errMgr.grammarError(ErrorType.UNKNOWN_SIMPLE_ATTRIBUTE,
g.fileName, x, x.getText(), expr);
}
new AttributeChecks(g, r, alt, node, rhs).examineAction();
}
@Override
public void attr(String expr, Token x) {
if ( g.isLexer() ) {
errMgr.grammarError(ErrorType.ATTRIBUTE_IN_LEXER_ACTION,
g.fileName, x, x.getText(), expr);
return;
}
if ( node.resolver.resolveToAttribute(x.getText(), node)==null ) {
if ( node.resolver.resolvesToToken(x.getText(), node) ) {
return; // $ID for token ref or label of token

View File

@ -148,6 +148,7 @@ public enum ErrorType {
IMPLICIT_TOKEN_DEFINITION(125, "implicit definition of token <arg> in parser", ErrorSeverity.WARNING),
IMPLICIT_STRING_DEFINITION(126, "cannot create implicit token for string literal <arg> in non-combined grammar", ErrorSeverity.ERROR),
// ALIAS_REASSIGNMENT(127, "token literal <arg> aliased to new token name <arg2>", ErrorSeverity.WARNING),
ATTRIBUTE_IN_LEXER_ACTION(128, "attribute references not allowed in lexer actions: $<arg>", ErrorSeverity.ERROR),
/** Documentation comment is unterminated */
//UNTERMINATED_DOC_COMMENT(, "", ErrorSeverity.ERROR),

View File

@ -107,9 +107,6 @@ public class Grammar implements AttributeResolver {
public static Map<String, AttributeDict> grammarAndLabelRefTypeToScope =
new HashMap<String, AttributeDict>() {{
put("lexer:RULE_LABEL", Rule.predefinedLexerRulePropertiesDict);
// put("lexer:LEXER_STRING_LABEL", Rule.predefinedLexerRulePropertiesDict);
// put("lexer:TOKEN_LABEL", AttributeDict.predefinedTokenDict);
put("parser:RULE_LABEL", Rule.predefinedRulePropertiesDict);
put("parser:TOKEN_LABEL", AttributeDict.predefinedTokenDict);
put("combined:RULE_LABEL", Rule.predefinedRulePropertiesDict);

View File

@ -58,14 +58,6 @@ public class Rule implements AttributeResolver {
add(new Attribute("ctx"));
}};
public static AttributeDict predefinedLexerRulePropertiesDict =
new AttributeDict(AttributeDict.DictType.PREDEFINED_LEXER_RULE) {{
add(new Attribute("text"));
add(new Attribute("type"));
add(new Attribute("channel"));
add(new Attribute("mode"));
}};
public static Set<String> validLexerCommands = new HashSet<String>() {{
// CALLS
add("mode");