org.antlr.v4.runtime.misc.IntervalSet Java Examples

The following examples show how to use org.antlr.v4.runtime.misc.IntervalSet. 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: BeetlAntlrErrorStrategy.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void reportUnwantedToken(@NotNull Parser recognizer)
{
	if (inErrorRecoveryMode(recognizer))
	{
		return;
	}

	beginErrorCondition(recognizer);

	Token t = recognizer.getCurrentToken();
	String tokenName = getTokenErrorDisplay(t);
	IntervalSet expecting = getExpectedTokens(recognizer);
	String msg = "多余输入 " + tokenName + " 期望 " + expecting.toString(recognizer.getTokenNames());
	BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg);
	//		exception.token = this.getGrammarToken(t);
	exception.pushToken(this.getGrammarToken(t));
	throw exception;
}
 
Example #2
Source File: GrammarIssuesCollector.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static Map<String,GrammarAST> getUnusedParserRules(Grammar g) {
        if ( g.ast==null || g.isLexer() ) return null;
        List<GrammarAST> ruleNodes = g.ast.getNodesWithTypePreorderDFS(IntervalSet.of(ANTLRParser.RULE_REF));
        // in case of errors, we walk AST ourselves
        // ANTLR's Grammar object might have bailed on rule defs etc...
        Set<String> ruleRefs = new HashSet<String>();
        Map<String,GrammarAST> ruleDefs = new HashMap<String,GrammarAST>();
        for (GrammarAST x : ruleNodes) {
            if ( x.getParent().getType()==ANTLRParser.RULE ) {
//				System.out.println("def "+x);
                ruleDefs.put(x.getText(), x);
            }
            else if ( x instanceof RuleRefAST) {
                RuleRefAST r = (RuleRefAST) x;
//				System.out.println("ref "+r);
                ruleRefs.add(r.getText());
            }
        }
        ruleDefs.keySet().removeAll(ruleRefs);
        return ruleDefs;
    }
 
Example #3
Source File: CSSErrorStrategy.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Consumes token until lexer state is function-balanced and
 * token from follow is matched.
 */
public void consumeUntil(Parser recognizer, IntervalSet follow, CSSLexerState.RecoveryMode mode, CSSLexerState ls) {
    CSSToken t;
    boolean finish;
    TokenStream input = recognizer.getInputStream();
    do {
        Token next = input.LT(1);
        if (next instanceof CSSToken) {
            t = (CSSToken) input.LT(1);
            if (t.getType() == Token.EOF) {
                logger.trace("token eof ");
                break;
            }
        } else
            break; /* not a CSSToken, probably EOF */
        // consume token if does not match
        finish = (t.getLexerState().isBalanced(mode, ls, t) && follow.contains(t.getType()));
        if (!finish) {
            logger.trace("Skipped: {}", t);
            input.consume();
        }
    } while (!finish);
}
 
Example #4
Source File: CSSErrorStrategy.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Consumes token until lexer state is function-balanced and
 * token from follow is matched. Matched token is also consumed
 */
protected void consumeUntilGreedy(Parser recognizer, IntervalSet set, CSSLexerState.RecoveryMode mode) {
    CSSToken t;
    do {
        Token next = recognizer.getInputStream().LT(1);
        if (next instanceof CSSToken) {
            t = (CSSToken) recognizer.getInputStream().LT(1);
            if (t.getType() == Token.EOF) {
                logger.trace("token eof ");
                break;
            }
        } else
            break; /* not a CSSToken, probably EOF */
        logger.trace("Skipped greedy: {}", t.getText());
        // consume token even if it will match
        recognizer.consume();
    }
    while (!(t.getLexerState().isBalanced(mode, null, t) && set.contains(t.getType())));
}
 
Example #5
Source File: BatfishANTLRErrorStrategy.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Consume all tokens a whole line at a time until the next token is one expected by the current
 * rule. Each line (as delimited by supplied separator token) starting from the current line up to
 * the last line consumed is placed in an {@link ErrorNode} and inserted as a child of the current
 * rule.
 *
 * @param recognizer The {@link Parser} to whom to delegate creation of each {@link ErrorNode}
 */
private void consumeBlocksUntilWanted(Parser recognizer) {
  IntervalSet expecting = recognizer.getExpectedTokens();
  IntervalSet whatFollowsLoopIterationOrRule = expecting.or(getErrorRecoverySet(recognizer));

  int nextToken;
  do {
    // Eat tokens until we are at the end of the line
    consumeUntilEndOfLine(recognizer);

    // Get the line number and separator text from the separator token
    Token separatorToken = recognizer.getCurrentToken();

    // Insert the current line as an {@link ErrorNode} as a child of the current rule
    createErrorNode(recognizer, recognizer.getContext(), separatorToken);

    // Eat the separator token
    recognizer.consume();

    nextToken = recognizer.getInputStream().LA(1);
  } while (!whatFollowsLoopIterationOrRule.contains(nextToken) && nextToken != Lexer.EOF);
}
 
Example #6
Source File: LL1OptionalBlockSingleAlt.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public LL1OptionalBlockSingleAlt(OutputModelFactory factory,
									 GrammarAST blkAST,
									 List<CodeBlockForAlt> alts)
	{
		super(factory, blkAST, alts);
		this.decision = ((DecisionState)blkAST.atnState).decision;

		/** Lookahead for each alt 1..n */
//		IntervalSet[] altLookSets = LinearApproximator.getLL1LookaheadSets(dfa);
		IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision);
		altLook = getAltLookaheadAsStringLists(altLookSets);
		IntervalSet look = altLookSets[0];
		IntervalSet followLook = altLookSets[1];

		IntervalSet expecting = look.or(followLook);
		this.error = getThrowNoViableAlt(factory, blkAST, expecting);

		expr = addCodeForLookaheadTempVar(look);
		followExpr = factory.getLL1Test(followLook, blkAST);
	}
 
Example #7
Source File: TestSetInline.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static Bitset[] createBitsets(OutputModelFactory factory,
									  IntervalSet set,
									  int wordSize,
									  boolean useZeroOffset) {
	List<Bitset> bitsetList = new ArrayList<Bitset>();
	for (int ttype : set.toArray()) {
		Bitset current = !bitsetList.isEmpty() ? bitsetList.get(bitsetList.size() - 1) : null;
		if (current == null || ttype > (current.shift + wordSize-1)) {
			current = new Bitset();
			if (useZeroOffset && ttype >= 0 && ttype < wordSize-1) {
				current.shift = 0;
			}
			else {
				current.shift = ttype;
			}

			bitsetList.add(current);
		}

		current.ttypes.add(factory.getGenerator().getTarget().getTokenTypeAsTargetLabel(factory.getGrammar(), ttype));
	}

	return bitsetList.toArray(new Bitset[bitsetList.size()]);
}
 
Example #8
Source File: LeftRecursiveRuleAnalyzer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public AltAST addPrecedenceArgToRules(AltAST t, int prec) {
	if ( t==null ) return null;
	// get all top-level rule refs from ALT
	List<GrammarAST> outerAltRuleRefs = t.getNodesWithTypePreorderDFS(IntervalSet.of(RULE_REF));
	for (GrammarAST x : outerAltRuleRefs) {
		RuleRefAST rref = (RuleRefAST)x;
		boolean recursive = rref.getText().equals(ruleName);
		boolean rightmost = rref == outerAltRuleRefs.get(outerAltRuleRefs.size()-1);
		if ( recursive && rightmost ) {
			GrammarAST dummyValueNode = new GrammarAST(new CommonToken(ANTLRParser.INT, ""+prec));
			rref.setOption(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME, dummyValueNode);
		}
	}
	return t;
}
 
Example #9
Source File: Choice.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public List<String[]> getAltLookaheadAsStringLists(IntervalSet[] altLookSets) {
	List<String[]> altLook = new ArrayList<String[]>();
	for (IntervalSet s : altLookSets) {
		altLook.add(factory.getGenerator().getTarget().getTokenTypesAsTargetLabels(factory.getGrammar(), s.toArray()));
	}
	return altLook;
}
 
Example #10
Source File: LL1PlusBlockSingleAlt.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public LL1PlusBlockSingleAlt(OutputModelFactory factory, GrammarAST plusRoot, List<CodeBlockForAlt> alts) {
	super(factory, plusRoot, alts);

	BlockAST blkAST = (BlockAST)plusRoot.getChild(0);
	PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState;

	stateNumber = blkStart.loopBackState.stateNumber;
	blockStartStateNumber = blkStart.stateNumber;
	PlusBlockStartState plus = (PlusBlockStartState)blkAST.atnState;
	this.decision = plus.loopBackState.decision;
	IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision);

	IntervalSet loopBackLook = altLookSets[0];
	loopExpr = addCodeForLoopLookaheadTempVar(loopBackLook);
}
 
Example #11
Source File: TestSetInline.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public TestSetInline(OutputModelFactory factory, GrammarAST ast, IntervalSet set, int wordSize) {
	super(factory, ast);
	bitsetWordSize = wordSize;
	Bitset[] withZeroOffset = createBitsets(factory, set, wordSize, true);
	Bitset[] withoutZeroOffset = createBitsets(factory, set, wordSize, false);
	this.bitsets = withZeroOffset.length <= withoutZeroOffset.length ? withZeroOffset : withoutZeroOffset;
	this.varName = "_la";
}
 
Example #12
Source File: Choice.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public TestSetInline addCodeForLookaheadTempVar(IntervalSet look) {
	List<SrcOp> testOps = factory.getLL1Test(look, ast);
	TestSetInline expr = Utils.find(testOps, TestSetInline.class);
	if (expr != null) {
		Decl d = new TokenTypeDecl(factory, expr.varName);
		factory.getCurrentRuleFunction().addLocalDecl(d);
		CaptureNextTokenType nextType = new CaptureNextTokenType(factory,expr.varName);
		addPreambleOp(nextType);
	}
	return expr;
}
 
Example #13
Source File: ThrowRecognitionException.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ThrowRecognitionException(OutputModelFactory factory, GrammarAST ast, IntervalSet expecting) {
		super(factory, ast);
		//this.decision = ((BlockStartState)ast.ATNState).decision;
		grammarLine = ast.getLine();
		grammarLine = ast.getCharPositionInLine();
		grammarFile = factory.getGrammar().fileName;
		//this.expecting = factory.createExpectingBitSet(ast, decision, expecting, "error");
//		factory.defineBitSet(this.expecting);
	}
 
Example #14
Source File: LL1AltBlock.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public LL1AltBlock(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlockForAlt> alts) {
	super(factory, blkAST, alts);
	this.decision = ((DecisionState)blkAST.atnState).decision;

	/** Lookahead for each alt 1..n */
	IntervalSet[] altLookSets = factory.getGrammar().decisionLOOK.get(decision);
	altLook = getAltLookaheadAsStringLists(altLookSets);

	IntervalSet expecting = IntervalSet.or(altLookSets); // combine alt sets
	this.error = getThrowNoViableAlt(factory, blkAST, expecting);
}
 
Example #15
Source File: LL1Loop.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public SrcOp addCodeForLoopLookaheadTempVar(IntervalSet look) {
	TestSetInline expr = addCodeForLookaheadTempVar(look);
	if (expr != null) {
		CaptureNextTokenType nextType = new CaptureNextTokenType(factory, expr.varName);
		addIterationOp(nextType);
	}
	return expr;
}
 
Example #16
Source File: Sync.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Sync(OutputModelFactory factory,
				GrammarAST blkOrEbnfRootAST,
				IntervalSet expecting,
				int decision,
				String position)
	{
		super(factory, blkOrEbnfRootAST);
		this.decision = decision;
//		this.expecting = factory.createExpectingBitSet(ast, decision, expecting, position);
//		factory.defineBitSet(this.expecting);
	}
 
Example #17
Source File: RangeUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the range intervals listed in {@code subtrahend} from {@code minuend}.
 */
@SuppressWarnings("checkstyle:LocalVariableName")
public static List<Range> subtractRanges(List<Range> minuend, List<Range> subtrahend) {
  IntervalSet iMinuend = intervalsToIntervalSet(rangesToIntervals(minuend));
  IntervalSet iSubtrahend = intervalsToIntervalSet(rangesToIntervals(subtrahend));
  IntervalSet iDifference = IntervalSet.subtract(iMinuend, iSubtrahend);
  return intervalSetToRanges(iDifference);
}
 
Example #18
Source File: RangeUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
private static IntervalSet intervalsToIntervalSet(List<Interval> intervals) {
  IntervalSet intervalSet = new IntervalSet();
  for (Interval interval : intervals) {
    intervalSet.add(interval.a, interval.b);
  }
  return intervalSet;
}
 
Example #19
Source File: ErrorStrategyAdaptor.java    From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void consumeUntil(Parser recognizer, IntervalSet set) {
	Token o = recognizer.getCurrentToken();
	if ( o.getType()==Token.EOF ) {
		recognizer.getRuleContext().addErrorNode(o);
	}
	super.consumeUntil(recognizer, set);
}
 
Example #20
Source File: BailMidwayThroughErrorStrategy.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
	protected void consumeUntil(Parser recognizer, IntervalSet set) {
//		System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
		int ttype = recognizer.getInputStream().LA(1);
		while (ttype != Token.EOF ) {
            //System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
//			recognizer.getInputStream().consume();
            recognizer.consume();
            ttype = recognizer.getInputStream().LA(1);
        }
	}
 
Example #21
Source File: BeetlAntlrErrorStrategy.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void reportMissingToken(@NotNull Parser recognizer)
	{
		if (inErrorRecoveryMode(recognizer))
		{
			return;
		}

		beginErrorCondition(recognizer);

//		Token t = recognizer.getCurrentToken();
		Token t = recognizer.getTokenStream().LT(-1);
		IntervalSet expecting = getExpectedTokens(recognizer);
		String expect = expecting.toString(recognizer.getTokenNames());
		if(expects.containsKey(expect)){
			expect = expects.get(expect);
		}
		if(expect.equals("'>>'")){
			expect = "'模板的占位结束符号'";
		}
		
		
		String tokenStr = getTokenErrorDisplay(t);
		String msg = null;
		if(expect.equals("'}'")&&tokenStr.equals("'>>'")) {
			 msg = "试图在第"+t.getLine()+"行未找到 '{' 匹配的结束符号 '}'";
		}else {
			//常规情况
			 msg = "缺少输入 " + expect + " 在 " + tokenStr+" 后面";
		}
		
		

		BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg);
		exception.pushToken(this.getGrammarToken(t));
		throw exception;
	}
 
Example #22
Source File: AnalysisPipeline.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void processLexer() {
	// make sure all non-fragment lexer rules must match at least one symbol
	for (Rule rule : g.rules.values()) {
		if (rule.isFragment()) {
			continue;
		}

		LL1Analyzer analyzer = new LL1Analyzer(g.atn);
		IntervalSet look = analyzer.LOOK(g.atn.ruleToStartState[rule.index], null);
		if (look.contains(Token.EPSILON)) {
			g.tool.errMgr.grammarError(ErrorType.EPSILON_TOKEN, g.fileName, ((GrammarAST)rule.ast.getChild(0)).getToken(), rule.name);
		}
	}
}
 
Example #23
Source File: AccumulatingErrorListener.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
private static String getExpected(IntervalSet expectedTokens) {
  List<String> sortedNames = expectedTokens
      .getIntervals()
      .stream()
      .flatMap(i -> IntStream.rangeClosed(i.a, i.b).boxed())
      .flatMap(TokenName::namesForToken)
      .sorted()
      .distinct()
      .map(TokenName::displayName)
      .collect(Collectors.toList());

  StringBuilder builder = new StringBuilder();
  int count = sortedNames.size();
  for (int i = 0; i < count; ++i) {
    builder.append(sortedNames.get(i));
    if (i < (count - 2)) {
      builder.append(", ");
    } else if (i == (count - 2)) {
      if (count >= 3) {
        builder.append(',');
      }
      builder.append(" or ");
    }
  }

  return builder.toString();
}
 
Example #24
Source File: ErrorHandler.java    From presto with Apache License 2.0 5 votes vote down vote up
private Set<String> getTokenNames(IntervalSet tokens)
{
    Set<String> names = new HashSet<>();
    for (int i = 0; i < tokens.size(); i++) {
        int token = tokens.get(i);
        if (token == Token.EOF) {
            names.add("<EOF>");
        }
        else {
            names.add(specialTokens.getOrDefault(token, vocabulary.getDisplayName(token)));
        }
    }

    return names;
}
 
Example #25
Source File: AccumulatingErrorListener.java    From cava with Apache License 2.0 5 votes vote down vote up
private static String getExpected(IntervalSet expectedTokens) {
  List<String> sortedNames = expectedTokens
      .getIntervals()
      .stream()
      .flatMap(i -> IntStream.rangeClosed(i.a, i.b).boxed())
      .flatMap(TokenName::namesForToken)
      .sorted()
      .distinct()
      .map(TokenName::displayName)
      .collect(Collectors.toList());

  StringBuilder builder = new StringBuilder();
  int count = sortedNames.size();
  for (int i = 0; i < count; ++i) {
    builder.append(sortedNames.get(i));
    if (i < (count - 2)) {
      builder.append(", ");
    } else if (i == (count - 2)) {
      if (count >= 3) {
        builder.append(',');
      }
      builder.append(" or ");
    }
  }

  return builder.toString();
}
 
Example #26
Source File: KsqlParserErrorStrategy.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
protected void reportUnwantedToken(Parser recognizer) {
  if (!this.inErrorRecoveryMode(recognizer)) {
    this.beginErrorCondition(recognizer);
    Token t = recognizer.getCurrentToken();
    String tokenName = this.getTokenErrorDisplay(t);
    IntervalSet expecting = this.getExpectedTokens(recognizer);
    String msg =
        "extraneous input " + tokenName + " expecting "
        + expecting.toString(recognizer.getVocabulary());
    recognizer.notifyErrorListeners(t, msg, (RecognitionException) null);
  }
}
 
Example #27
Source File: KsqlParserErrorStrategy.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
protected void reportMissingToken(Parser recognizer) {
  if (!this.inErrorRecoveryMode(recognizer)) {
    this.beginErrorCondition(recognizer);
    Token t = recognizer.getCurrentToken();
    IntervalSet expecting = this.getExpectedTokens(recognizer);
    String msg =
        "missing " + expecting.toString(recognizer.getVocabulary()) + " at " + this
            .getTokenErrorDisplay(t);
    recognizer.notifyErrorListeners(t, msg, (RecognitionException) null);
  }
}
 
Example #28
Source File: GyroErrorStrategyTest.java    From gyro with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void beforeEach() {
    strategy = spy(GyroErrorStrategy.INSTANCE);
    recognizer = mock(Parser.class);
    token = mock(Token.class);
    set = mock(IntervalSet.class);
}
 
Example #29
Source File: ClawPragma.java    From claw-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Get a readable list of token found in an IntervalSet.
 *
 * @param set    Set of tokens to be found.
 * @param parser Current parser instance.
 * @return List of human readable tokens.
 */
private static List<String> getTokens(IntervalSet set, ClawParser parser) {
  List<String> tokens = new ArrayList<>();
  for(int tokenId : set.toList()) {
    if(parser.getVocabulary().getLiteralName(tokenId) == null) {
      tokens.add(parser.getVocabulary().getDisplayName(tokenId));
    } else {
      tokens.add(parser.getVocabulary().getLiteralName(tokenId));
    }
  }
  return tokens;
}
 
Example #30
Source File: ExpressionInterpreter.java    From arma-dialog-creator with MIT License 5 votes vote down vote up
@Override
public void reportMissingToken(Parser recognizer) {
	beginErrorCondition(recognizer);
	Token t = recognizer.getCurrentToken();
	IntervalSet expecting = getExpectedTokens(recognizer);
	String msg = "missing " + expecting.toString(recognizer.getTokenNames()) + " at " + getTokenErrorDisplay(t);
	throw new RecognitionException(msg, recognizer, recognizer.getInputStream(), recognizer.getContext());
}