Top Description Fields Constructors Methods
org.antlr.v4.semantics

public Class SymbolChecks

extends Object
Class Inheritance
Imports
org.antlr.runtime.tree.CommonTree, .Tree, org.antlr.v4.automata.LexerATNFactory, org.antlr.v4.parse.ANTLRLexer, .ANTLRParser, org.antlr.v4.runtime.Token, org.antlr.v4.tool.Alternative, .Attribute, .AttributeDict, .ErrorManager, .ErrorType, .Grammar, .LabelElementPair, .LabelType, .LeftRecursiveRule, .LexerGrammar, .Rule, org.antlr.v4.tool.ast.AltAST, .GrammarAST, .TerminalAST, java.util.ArrayList, .Collection, .HashMap, .HashSet, .List, .Map, .Set

Check for symbol problems; no side-effects. Inefficient to walk rules and such multiple times, but I like isolating all error checking outside of code that actually defines symbols etc... Side-effect: strip away redef'd rules.

Field Summary

Modifier and TypeField and Description
pack-priv Map<String, Set<String>>
pack-priv SymbolCollector
public ErrorManager
pack-priv Grammar
g

pack-priv Map<String, Rule>
protected final Set<String>
pack-priv Set<String>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
protected void
checkDeclarationRuleConflicts(Rule r, AttributeDict attributes, Set<String> ruleNames, ErrorType errorType)

public void
public void
public void
checkForLabelConflicts(Collection<Rule> rules)

Make sure a label doesn't conflict with another symbol.

public void
private void
checkForOverlap(Grammar g, Rule rule1, Rule rule2, List<String> firstTokenStringValues, List<String> secondTokenStringValues)

For same rule compare values from next index: TOKEN_WITH_SAME_VALUES: 'asdf' | 'asdf'; For different rules compare from start value: TOKEN1: 'asdf'; TOKEN2: 'asdf';

public void
private void
public void
checkForUnreachableTokens(Grammar g)

Algorithm steps: 1.

private void
protected void
checkLocalConflictingDeclarations(Rule r, AttributeDict attributes, AttributeDict referenceAttributes, ErrorType errorType)

protected void
public void
private String
private List<String>
getSingleTokenValues(Rule rule)

Returns .

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

actionScopeToActionNamesback to summary
pack-priv Map<String, Set<String>> actionScopeToActionNames
collectorback to summary
pack-priv SymbolCollector collector
errMgrback to summary
public ErrorManager errMgr
gback to summary
pack-priv Grammar g
nameToRuleMapback to summary
pack-priv Map<String, Rule> nameToRuleMap
reservedNamesback to summary
protected final Set<String> reservedNames
tokenIDsback to summary
pack-priv Set<String> tokenIDs

Constructor Detail

SymbolChecksback to summary
public SymbolChecks(Grammar g, SymbolCollector collector)

Method Detail

checkActionRedefinitionsback to summary
public void checkActionRedefinitions(List<GrammarAST> actions)
checkDeclarationRuleConflictsback to summary
protected void checkDeclarationRuleConflicts(Rule r, AttributeDict attributes, Set<String> ruleNames, ErrorType errorType)
checkForAttributeConflictsback to summary
public void checkForAttributeConflicts(Rule r)
checkForLabelConflictback to summary
public void checkForLabelConflict(Rule r, GrammarAST labelID)
checkForLabelConflictsback to summary
public void checkForLabelConflicts(Collection<Rule> rules)

Make sure a label doesn't conflict with another symbol. Labels must not conflict with: rules, tokens, scope names, return values, parameters, and rule-scope dynamic attributes defined in surrounding rule. Also they must have same type for repeated defs.

checkForModeConflictsback to summary
public void checkForModeConflicts(Grammar g)
checkForOverlapback to summary
private void checkForOverlap(Grammar g, Rule rule1, Rule rule2, List<String> firstTokenStringValues, List<String> secondTokenStringValues)

For same rule compare values from next index: TOKEN_WITH_SAME_VALUES: 'asdf' | 'asdf'; For different rules compare from start value: TOKEN1: 'asdf'; TOKEN2: 'asdf';

checkForQualifiedRuleIssuesback to summary
public void checkForQualifiedRuleIssues(Grammar g, List<GrammarAST> qualifiedRuleRefs)
checkForTypeMismatchback to summary
private void checkForTypeMismatch(Rule r, LabelElementPair prevLabelPair, LabelElementPair labelPair)
checkForUnreachableTokensback to summary
public void checkForUnreachableTokens(Grammar g)

Algorithm steps: 1. Collect all simple string literals (i.e. 'asdf', 'as' 'df', but not [a-z]+, 'a'..'z') for all lexer rules in each mode except of autogenerated tokens (getSingleTokenValues) 2. Compare every string literal with each other (checkForOverlap) and throw TOKEN_UNREACHABLE warning if the same string found. Complexity: O(m * n^2 / 2), approximately equals to O(n^2) where m - number of modes, n - average number of lexer rules per mode. See also testUnreachableTokens unit test for details.

checkLabelPairsback to summary
private void checkLabelPairs(Rule r, Map<String, LabelElementPair> labelNameSpace, List<LabelElementPair> pairs)
checkLocalConflictingDeclarationsback to summary
protected void checkLocalConflictingDeclarations(Rule r, AttributeDict attributes, AttributeDict referenceAttributes, ErrorType errorType)
checkReservedNamesback to summary
protected void checkReservedNames(Collection<Rule> rules)
checkRuleArgsback to summary
public void checkRuleArgs(Grammar g, List<GrammarAST> rulerefs)
findAltLabelNameback to summary
private String findAltLabelName(CommonTree label)
getSingleTokenValuesback to summary
private List<String> getSingleTokenValues(Rule rule)

Returns . list of simple string literals for rule {@param rule}

processback to summary
public void process()