List of usage examples for org.antlr.v4.runtime ParserRuleContext getText
@Override
public String getText()
From source file:nl.lxtreme.libtdl.grammar.Util.java
License:Apache License
/** * Validates whether a given context denotes a numeric value in the given * range. In case the value falls outside the defined range, a problem will * be added to the given problem reporter. * //ww w . j ava2 s . com * @param ctx * the parser context that denotes the numeric value to test; * @param lower * the lower bound (inclusive!) of the range that is valid; * @param upper * the upper bound (inclusive!) of the range that is valid; * @param msg * the message to add as problem marker in case the value is * invalid; * @param reporter * the problem reporter to add the marker to. * @return the parsed numeric value of the given parser context, or * <code>null</code> in case it did not denote a valid numeric * value (not inside the given range, or otherwise invalid). */ public static Long validateValue(ParserRuleContext ctx, long lower, long upper, String msg, ProblemReporter reporter) { String text = null; if (ctx != null) { text = ctx.getText(); } Long value = decode(text); if (value == null) { return null; } if (!inRange(value.longValue(), lower, upper)) { int offset = ctx.getStart().getStartIndex(); int length = ctx.getStop().getStopIndex() - offset; MarkerBuilder builder = new MarkerBuilder(); Marker marker = builder.setCategory(Category.SEMANTIC).setType(Type.ERROR) .setLocation(offset, length, ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine()) .setDescription(msg).build(); reporter.report(marker); return null; } return value; }
From source file:notaql.parser.NotaQLErrorListener.java
License:Apache License
public static void printContextStack(ParserRuleContext ctx) { if (ctx.getParent() != null) printContextStack(ctx.getParent()); System.err.println("context: " + ctx.getText() + " (" + ctx.getClass().toString() + ")"); }
From source file:org.apache.sysml.parser.common.CommonSyntacticValidator.java
License:Apache License
protected void constDoubleIdExpressionHelper(ParserRuleContext ctx, ExpressionInfo me) { try {/*from w w w . j a v a 2 s . co m*/ double val = Double.parseDouble(ctx.getText()); me.expr = new DoubleIdentifier(ctx, val, currentFile); } catch (Exception e) { notifyErrorListeners("cannot parse the float value: \'" + ctx.getText() + "\'", ctx.getStart()); return; } }
From source file:org.apache.sysml.parser.common.CommonSyntacticValidator.java
License:Apache License
protected void constIntIdExpressionHelper(ParserRuleContext ctx, ExpressionInfo me) { try {//from w w w . j av a 2 s . c o m long val = Long.parseLong(ctx.getText()); me.expr = new IntIdentifier(ctx, val, currentFile); } catch (Exception e) { notifyErrorListeners("cannot parse the int value: \'" + ctx.getText() + "\'", ctx.getStart()); return; } }
From source file:org.apache.sysml.parser.common.CommonSyntacticValidator.java
License:Apache License
protected void constStringIdExpressionHelper(ParserRuleContext ctx, ExpressionInfo me) { String val = extractStringInQuotes(ctx.getText(), true); if (val == null) { notifyErrorListeners("incorrect string literal ", ctx.start); return;/*from w w w . ja v a2 s. c om*/ } me.expr = new StringIdentifier(ctx, val, currentFile); }
From source file:org.apache.sysml.parser.Expression.java
License:Apache License
/** * Set ParserRuleContext values (begin line, begin column, end line, end * column, and text).//from w w w . ja va 2 s.c om * * @param ctx * the antlr ParserRuleContext */ public void setCtxValues(ParserRuleContext ctx) { setBeginLine(ctx.start.getLine()); setBeginColumn(ctx.start.getCharPositionInLine()); setEndLine(ctx.stop.getLine()); setEndColumn(ctx.stop.getCharPositionInLine()); // preserve whitespace if possible if ((ctx.start != null) && (ctx.stop != null) && (ctx.start.getStartIndex() != -1) && (ctx.stop.getStopIndex() != -1) && (ctx.start.getStartIndex() <= ctx.stop.getStopIndex()) && (ctx.start.getInputStream() != null)) { String text = ctx.start.getInputStream() .getText(Interval.of(ctx.start.getStartIndex(), ctx.stop.getStopIndex())); if (text != null) { text = text.trim(); } setText(text); } else { String text = ctx.getText(); if (text != null) { text = text.trim(); } setText(text); } }
From source file:org.apache.sysml.parser.ParseInfo.java
License:Apache License
public static ParseInfo ctxAndFilenameToParseInfo(ParserRuleContext ctx, String filename) { ParseInfo pi = new ParseInfo() { private int beginLine; private int beginColumn; private int endLine; private int endColumn; private String text; private String filename; @Override/*w w w .j ava 2s . c o m*/ public void setBeginLine(int beginLine) { this.beginLine = beginLine; } @Override public void setBeginColumn(int beginColumn) { this.beginColumn = beginColumn; } @Override public void setEndLine(int endLine) { this.endLine = endLine; } @Override public void setEndColumn(int endColumn) { this.endColumn = endColumn; } @Override public void setText(String text) { this.text = text; } @Override public void setFilename(String filename) { this.filename = filename; } @Override public int getBeginLine() { return beginLine; } @Override public int getBeginColumn() { return beginColumn; } @Override public int getEndLine() { return endLine; } @Override public int getEndColumn() { return endColumn; } @Override public String getText() { return text; } @Override public String getFilename() { return filename; } }; pi.setBeginLine(ctx.start.getLine()); pi.setBeginColumn(ctx.start.getCharPositionInLine()); pi.setEndLine(ctx.stop.getLine()); pi.setEndColumn(ctx.stop.getCharPositionInLine()); // preserve whitespace if possible if ((ctx.start != null) && (ctx.stop != null) && (ctx.start.getStartIndex() != -1) && (ctx.stop.getStopIndex() != -1) && (ctx.start.getStartIndex() <= ctx.stop.getStopIndex()) && (ctx.start.getInputStream() != null)) { String text = ctx.start.getInputStream() .getText(Interval.of(ctx.start.getStartIndex(), ctx.stop.getStopIndex())); if (text != null) { text = text.trim(); } pi.setText(text); } else { String text = ctx.getText(); if (text != null) { text = text.trim(); } pi.setText(text); } pi.setFilename(filename); return pi; }
From source file:org.eclipse.titan.common.parsers.ParserLogger.java
License:Open Source License
/** * Rule exception info in string format for logging purpose * @param aRule rule//w w w . ja va 2 s . c o m * @param aTokenNameResolver resolver to get token name * @return exception stack trace + some other info from the exception object */ private static String getExceptionInfo(final ParserRuleContext aRule, final TokenNameResolver aTokenNameResolver) { final RecognitionException e = aRule.exception; if (e == null) { return ""; } final StringBuilder sb = new StringBuilder(); sb.append("\naRule.getText() == " + aRule.getText()); sb.append("\ngetOffendingState() == " + e.getOffendingState()); sb.append("\ngetExpectedTokens() == ["); final List<Integer> expectedTokens = e.getExpectedTokens().toList(); for (int i = 0; i < expectedTokens.size(); i++) { if (i > 0) { sb.append(", "); } final int tokenType = expectedTokens.get(i); sb.append(getTokenName(tokenType, aTokenNameResolver)); } sb.append("]"); if (e instanceof NoViableAltException) { NoViableAltException nvae = (NoViableAltException) e; sb.append("\ngetStartToken() == " + getTokenInfo(nvae.getStartToken(), aTokenNameResolver)); sb.append("\ngetDeadEndConfigs() == " + nvae.getDeadEndConfigs()); } final StringWriter errors = new StringWriter(); e.printStackTrace(new PrintWriter(errors)); sb.append("\n" + errors.toString()); return sb.toString(); }
From source file:org.elasticsearch.painless.Metadata.java
License:Apache License
/** * Retrieves StatementMetadata from the statementMetadata map. * @param source The ANTLR node for this metadata. * @return The retrieved StatementMetadata. *///ww w . j a v a 2s . c o m StatementMetadata getStatementMetadata(final ParserRuleContext source) { final StatementMetadata sourcesmd = statementMetadata.get(source); if (sourcesmd == null) { throw new IllegalStateException(error(source) + "Statement metadata does not exist at" + " the parse node with text [" + source.getText() + "]."); } return sourcesmd; }
From source file:org.elasticsearch.painless.Metadata.java
License:Apache License
/** * Retrieves ExpressionMetadata from the expressionMetadata map. * @param source The ANTLR node for this metadata. * @return The retrieved ExpressionMetadata. *///from w ww. j av a 2s. c o m ExpressionMetadata getExpressionMetadata(final ParserRuleContext source) { final ExpressionMetadata sourceemd = expressionMetadata.get(source); if (sourceemd == null) { throw new IllegalStateException(error(source) + "Expression metadata does not exist at" + " the parse node with text [" + source.getText() + "]."); } return sourceemd; }