org.antlr.v4.runtime.Vocabulary Java Examples

The following examples show how to use org.antlr.v4.runtime.Vocabulary. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Identifiers.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Set<Keyword> identifierCandidates() {
    HashSet<Keyword> candidates = new HashSet<>();
    Vocabulary vocabulary = SqlBaseLexer.VOCABULARY;
    for (int i = 0; i < vocabulary.getMaxTokenType(); i++) {
        String literal = vocabulary.getLiteralName(i);
        if (literal == null) {
            continue;
        }
        literal = literal.replace("'", "");

        Matcher matcher = IDENTIFIER.matcher(literal.toLowerCase(Locale.ENGLISH));
        if (matcher.matches()) {
            candidates.add(new Keyword(literal, reserved(literal)));
        }
    }
    return candidates;
}
 
Example #2
Source File: GyroErrorStrategyTest.java    From gyro with Apache License 2.0 5 votes vote down vote up
@Test
void reportInputMismatch() {
    InputMismatchException error = mock(InputMismatchException.class);

    when(error.getOffendingToken()).thenReturn(token);
    when(error.getExpectedTokens()).thenReturn(set);
    when(set.toString(any(Vocabulary.class))).thenReturn("foo");
    when(recognizer.getVocabulary()).thenReturn(mock(Vocabulary.class));

    strategy.reportInputMismatch(recognizer, error);

    verify(recognizer).notifyErrorListeners(token, "Expected foo", error);
}
 
Example #3
Source File: GrammarParserInterpreter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public GrammarParserInterpreter(Grammar g,
								String grammarFileName,
								Vocabulary vocabulary,
								Collection<String> ruleNames,
								ATN atn,
								TokenStream input) {
	super(grammarFileName, vocabulary, ruleNames, atn, input);
	this.g = g;
}
 
Example #4
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static TerminalNode getMatchingLeftSymbol(Corpus corpus,
                                                 InputDocument doc,
                                                 TerminalNode node)
{
	ParserRuleContext parent = (ParserRuleContext)node.getParent();
	int curTokensParentRuleIndex = parent.getRuleIndex();
	Token curToken = node.getSymbol();
	if (corpus.ruleToPairsBag != null) {
		String ruleName = doc.parser.getRuleNames()[curTokensParentRuleIndex];
		RuleAltKey ruleAltKey = new RuleAltKey(ruleName, parent.getAltNumber());
		List<Pair<Integer, Integer>> pairs = corpus.ruleToPairsBag.get(ruleAltKey);
		if ( pairs!=null ) {
			// Find appropriate pair given current token
			// If more than one pair (a,b) with b=current token pick first one
			// or if a common pair like ({,}), then give that one preference.
			// or if b is punctuation, prefer a that is punct
			List<Integer> viableMatchingLeftTokenTypes = viableLeftTokenTypes(parent, curToken, pairs);
			Vocabulary vocab = doc.parser.getVocabulary();
			if ( !viableMatchingLeftTokenTypes.isEmpty() ) {
				int matchingLeftTokenType =
					CollectTokenPairs.getMatchingLeftTokenType(curToken, viableMatchingLeftTokenTypes, vocab);
				List<TerminalNode> matchingLeftNodes = parent.getTokens(matchingLeftTokenType);
				// get matching left node by getting last node to left of current token
				List<TerminalNode> nodesToLeftOfCurrentToken =
					filter(matchingLeftNodes, n -> n.getSymbol().getTokenIndex()<curToken.getTokenIndex());
				TerminalNode matchingLeftNode = nodesToLeftOfCurrentToken.get(nodesToLeftOfCurrentToken.size()-1);
				if (matchingLeftNode == null) {
					System.err.println("can't find matching node for "+node.getSymbol());
				}
				return matchingLeftNode;
			}
		}
	}
	return null;
}
 
Example #5
Source File: ParserWrapper.java    From antlr4-autosuggest with Apache License 2.0 5 votes vote down vote up
public ParserWrapper(ParserFactory parserFactory, Vocabulary lexerVocabulary) {
    this.lexerVocabulary = lexerVocabulary;
    
    Parser parserForAtnOnly = parserFactory.createParser(null);
    this.parserAtn = parserForAtnOnly.getATN();
    this.parserRuleNames = parserForAtnOnly.getRuleNames();
    logger.debug("Parser rule names: " + StringUtils.join(parserForAtnOnly.getRuleNames(), ", "));
}
 
Example #6
Source File: ReservedIdentifiers.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Set<String> sqlKeywords()
{
    ImmutableSet.Builder<String> names = ImmutableSet.builder();
    Vocabulary vocabulary = SqlBaseLexer.VOCABULARY;
    for (int i = 0; i <= vocabulary.getMaxTokenType(); i++) {
        String name = nullToEmpty(vocabulary.getLiteralName(i));
        Matcher matcher = IDENTIFIER.matcher(name);
        if (matcher.matches()) {
            names.add(matcher.group(1));
        }
    }
    return names.build();
}
 
Example #7
Source File: GyroErrorStrategyTest.java    From gyro with Apache License 2.0 5 votes vote down vote up
@Test
void reportMissingToken() {
    when(recognizer.getCurrentToken()).thenReturn(token);
    when(recognizer.getExpectedTokens()).thenReturn(set);
    when(set.toString(any(Vocabulary.class))).thenReturn("foo");
    when(recognizer.getVocabulary()).thenReturn(mock(Vocabulary.class));

    strategy.reportMissingToken(recognizer);

    assertThat(strategy.inErrorRecoveryMode(recognizer)).isTrue();
    verify(recognizer).notifyErrorListeners(token, "Missing foo", null);
}
 
Example #8
Source File: ReservedIdentifiers.java    From rainbow with Apache License 2.0 5 votes vote down vote up
private static Set<String> possibleIdentifiers()
{
    ImmutableSet.Builder<String> names = ImmutableSet.builder();
    Vocabulary vocabulary = SqlBaseLexer.VOCABULARY;
    for (int i = 0; i <= vocabulary.getMaxTokenType(); i++) {
        String name = nullToEmpty(vocabulary.getLiteralName(i));
        Matcher matcher = IDENTIFIER.matcher(name);
        if (matcher.matches()) {
            names.add(matcher.group(1));
        }
    }
    return names.build();
}
 
Example #9
Source File: FunctionExpressionLexer.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Vocabulary getVocabulary() {
	return VOCABULARY;
}
 
Example #10
Source File: EditorConfigParser.java    From editorconfig-netbeans with MIT License 4 votes vote down vote up
@Override
public Vocabulary getVocabulary() {
  return VOCABULARY;
}
 
Example #11
Source File: EditorConfigLexer.java    From editorconfig-netbeans with MIT License 4 votes vote down vote up
@Override
public Vocabulary getVocabulary() {
  return VOCABULARY;
}
 
Example #12
Source File: XpathLexer.java    From JsoupXpath with Apache License 2.0 4 votes vote down vote up
@Override

	public Vocabulary getVocabulary() {
		return VOCABULARY;
	}
 
Example #13
Source File: XpathParser.java    From JsoupXpath with Apache License 2.0 4 votes vote down vote up
@Override

	public Vocabulary getVocabulary() {
		return VOCABULARY;
	}
 
Example #14
Source File: SoqlLexer.java    From components with Apache License 2.0 4 votes vote down vote up
@Override

	public Vocabulary getVocabulary() {
		return VOCABULARY;
	}
 
Example #15
Source File: SoqlParser.java    From components with Apache License 2.0 4 votes vote down vote up
@Override
public Vocabulary getVocabulary() {
	return VOCABULARY;
}
 
Example #16
Source File: XGBoostModelParser.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public Vocabulary getVocabulary() {
    return VOCABULARY;
}
 
Example #17
Source File: XGBoostModelLexer.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public Vocabulary getVocabulary() {
    return VOCABULARY;
}
 
Example #18
Source File: Grammar.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Gets a {@link Vocabulary} instance describing the vocabulary used by the
 * grammar.
 */

public Vocabulary getVocabulary() {
	return new VocabularyImpl(getTokenLiteralNames(), getTokenSymbolicNames());
}
 
Example #19
Source File: CollectTokenPairs.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public CollectTokenPairs(Vocabulary vocab, String[] ruleNames) {
	this.vocab = vocab;
	this.ruleNames = ruleNames;
}
 
Example #20
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static String _toString(FeatureMetaData[] FEATURES, InputDocument doc, int[] features,
                               boolean showInfo) {
	Vocabulary v = doc.parser.getVocabulary();
	String[] ruleNames = doc.parser.getRuleNames();
	StringBuilder buf = new StringBuilder();
	for (int i=0; i<FEATURES.length; i++) {
		if ( FEATURES[i].type.equals(UNUSED) ) continue;
		if ( i>0 ) buf.append(" ");
		if ( i==INDEX_CUR_TOKEN_TYPE ) {
			buf.append("| "); // separate prev from current tokens
		}
		int displayWidth = FEATURES[i].type.displayWidth;
		switch ( FEATURES[i].type ) {
			case TOKEN :
				String tokenName = v.getDisplayName(features[i]);
				String abbrev = StringUtils.abbreviateMiddle(tokenName, "*", displayWidth);
				String centered = StringUtils.center(abbrev, displayWidth);
				buf.append(String.format("%"+displayWidth+"s", centered));
				break;
			case RULE :
				if ( features[i]>=0 ) {
					String ruleName = ruleNames[unrulealt(features[i])[0]];
					int ruleAltNum = unrulealt(features[i])[1];
					ruleName += ":"+ruleAltNum;
					abbrev = StringUtils.abbreviateMiddle(ruleName, "*", displayWidth);
					buf.append(String.format("%"+displayWidth+"s", abbrev));
				}
				else {
					buf.append(Tool.sequence(displayWidth, " "));
				}
				break;
			case INT :
			case INFO_LINE:
			case INFO_CHARPOS:
				if ( showInfo ) {
					if ( features[i]>=0 ) {
						buf.append(String.format("%"+displayWidth+"s", StringUtils.center(String.valueOf(features[i]), displayWidth)));
					}
					else {
						buf.append(Tool.sequence(displayWidth, " "));
					}
				}
				break;
			case INFO_FILE:
				if ( showInfo ) {
					String fname = new File(doc.fileName).getName();
					fname = StringUtils.abbreviate(fname, displayWidth);
					buf.append(String.format("%"+displayWidth+"s", fname));
				}
				break;
			case BOOL :
				if ( features[i]!=-1 ) {
					buf.append(features[i] == 1 ? "true " : "false");
				}
				else {
					buf.append(Tool.sequence(displayWidth, " "));
				}
				break;
			default :
				System.err.println("NO STRING FOR FEATURE TYPE: "+ FEATURES[i].type);
		}
	}
	return buf.toString();
}
 
Example #21
Source File: AtlasDSLLexer.java    From atlas with Apache License 2.0 4 votes vote down vote up
@Override

	public Vocabulary getVocabulary() {
		return VOCABULARY;
	}
 
Example #22
Source File: JavascriptLexer.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override

  public Vocabulary getVocabulary() {
    return VOCABULARY;
  }
 
Example #23
Source File: SQLiteLexer.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override

	public Vocabulary getVocabulary() {
		return VOCABULARY;
	}
 
Example #24
Source File: QueryOptionsFactory.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
private CustomErrorListener(Vocabulary vocabulary) {
    this.vocabulary = vocabulary;
}
 
Example #25
Source File: LexerWrapper.java    From antlr4-autosuggest with Apache License 2.0 4 votes vote down vote up
public Vocabulary getVocabulary() {
    return getCachedLexer().getVocabulary();
}