List of usage examples for org.antlr.v4.runtime.misc Interval Interval
public Interval(int a, int b)
From source file:cloudlens.parser.ASTBuilder.java
License:Apache License
@Override public Void visitVarDecl(CloudLensParser.VarDeclContext ctx) { visitChildren(ctx);// w w w . j a va2s . c o m final int line = ctx.getStart().getLine(); final String ident = ctx.IDENT().getText(); final int a = ctx.start.getStartIndex(); final int b = ctx.stop.getStopIndex(); final Interval interval = new Interval(a, b); final CharStream source = ctx.start.getInputStream(); final String body = source.getText(interval); ctx.ast = new ASTDeclaration(fileName, line, ident, body); return null; }
From source file:cloudlens.parser.ASTBuilder.java
License:Apache License
@Override public Void visitBody(CloudLensParser.BodyContext ctx) { final int a = ctx.start.getStartIndex(); final int b = ctx.stop.getStopIndex(); // remove opening and closing {.} final Interval interval = new Interval(a + 1, b - 1); final CharStream source = ctx.start.getInputStream(); ctx.ast = source.getText(interval);//from w w w . ja v a 2 s .c o m return null; }
From source file:cloudlens.parser.ASTBuilder.java
License:Apache License
@Override public Void visitRegex(CloudLensParser.RegexContext ctx) { visitChildren(ctx);/* w ww .j a va2 s . c o m*/ final List<String> rg = new ArrayList<>(); if (ctx.IDENT() != null) { rg.add(ctx.IDENT().getText()); } else if (ctx.STRING() != null) { final int a = ctx.start.getStartIndex(); final int b = ctx.stop.getStopIndex(); // remove opening and closing "." final Interval interval = new Interval(a + 1, b - 1); final CharStream source = ctx.start.getInputStream(); final String r = source.getText(interval); rg.add("\"" + StringEscapeUtils.escapeEcmaScript(r) + "\""); } else if (ctx.regex() != null) { for (final RegexContext r : ctx.regex()) { rg.add(r.ast); } } ctx.ast = StringUtils.join(rg, "+"); return null; }
From source file:cloudlens.parser.ASTBuilder.java
License:Apache License
@Override public Void visitArgList(CloudLensParser.ArgListContext ctx) { final int a = ctx.start.getStartIndex(); final int b = ctx.stop.getStopIndex(); // remove opening and closing (.) final Interval interval = new Interval(a + 1, b - 1); final CharStream source = ctx.start.getInputStream(); ctx.ast = "[" + source.getText(interval) + "]"; return null;/*from w ww .j av a2 s. c o m*/ }
From source file:cloudlens.parser.ASTBuilder.java
License:Apache License
@Override public Void visitUrl(CloudLensParser.UrlContext ctx) { final int a = ctx.start.getStartIndex(); final int b = ctx.stop.getStopIndex(); // remove opening and closing "." final Interval interval = new Interval(a + 1, b - 1); final CharStream source = ctx.start.getInputStream(); ctx.ast = source.getText(interval);//from w w w . j av a 2 s . co m return null; }
From source file:com.github.jknack.handlebars.internal.HbsErrorReporter.java
License:Apache License
/** * Extract lines./*w ww. j a v a 2 s . com*/ * * @param recognizer A lexer/parser. * @return Source lines */ private String[] lines(final Recognizer<?, ?> recognizer) { IntStream stream = recognizer.getInputStream(); if (stream instanceof CommonTokenStream) { stream = ((CommonTokenStream) stream).getTokenSource().getInputStream(); } final String input; if (stream instanceof CharStream) { input = ((CharStream) stream).getText(new Interval(0, stream.size())); } else { input = stream.toString(); } String[] lines = input.split("\n"); return lines; }
From source file:com.jmcalc.utils.DescriptiveErrorListener.java
License:Apache License
@Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { CommonToken offendingToken = ((CommonToken) offendingSymbol); String sourceName = offendingToken.getInputStream() .getText(new Interval(0, offendingToken.getInputStream().size())); if (e != null) { throw new ExpressionParseException(sourceName + " -> line " + line + ":" + charPositionInLine + " mismatched input '" + e.getOffendingToken().getText() + "'"); } else {//w ww. j a v a2 s. com throw new ExpressionParseException(sourceName + " -> line " + line + ":" + charPositionInLine + " unexpected token: " + offendingToken.getText()); } }
From source file:edu.clemson.resolve.misc.Utils.java
License:BSD License
/** * Returns the raw text encapsulated by a {@link ParserRuleContext} exactly as it appears within whatever * sourcecode the user typed in.//from w w w . j av a 2s . c o m * * @param ctx the rule context * * @return the raw sourcecode represented by {@code ctx} * @deprecated use {@link Trees#getNodeText)} instead */ @Deprecated @NotNull public static String getRawText(@Nullable ParserRuleContext ctx) { if (ctx == null) return ""; Interval interval = new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()); return ctx.start.getInputStream().getText(interval); }
From source file:greycat.language.Model.java
License:Open Source License
private void internal_parse(CharStream in, Resolver resolver) { if (md == null) { try {//from w w w .j a va 2s .co m md = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } if (in.size() == 0) { return; } String sha1 = convertToHex(md.digest(in.getText(new Interval(0, in.size())).getBytes())); if (alreadyLoaded.contains(sha1)) { return; } else { //circular dependency protection alreadyLoaded.add(sha1); } BufferedTokenStream tokens = new CommonTokenStream(new GreyCatModelLexer(in)); GreyCatModelParser parser = new GreyCatModelParser(tokens); GreyCatModelParser.ModelDclContext modelDclCtx = parser.modelDcl(); //first subImport modelDclCtx.importDcl().forEach(importDclContext -> { String subPath = cleanString(importDclContext.path); CharStream subStream = resolver.resolver(subPath); if (subStream == null) { throw new RuntimeException("Import not resolved " + subPath); } try { internal_parse(subStream, resolver); } catch (Exception e) { throw new RuntimeException("Parse Error while parsing " + subPath, e); } }); // constants for (GreyCatModelParser.ConstDclContext constDclCtx : modelDclCtx.constDcl()) { String const_name = constDclCtx.name.getText(); Constant c = constants.get(const_name); if (c == null) { c = new Constant(const_name); constants.put(const_name, c); } c.setType(constDclCtx.type.getText()); String value = null; if (constDclCtx.constValueDcl() != null) { if (constDclCtx.constValueDcl().simpleValueDcl() != null) { GreyCatModelParser.SimpleValueDclContext sVc = constDclCtx.constValueDcl().simpleValueDcl(); if (sVc.STRING() != null) { value = cleanString(sVc.STRING().getSymbol()); } else { value = constDclCtx.constValueDcl().simpleValueDcl().getText(); } } else if (constDclCtx.constValueDcl().taskValueDcl() != null) { GreyCatModelParser.TaskValueDclContext taskDcl = constDclCtx.constValueDcl().taskValueDcl(); value = taskDcl.getText(); } } c.setValue(value); } // classes for (GreyCatModelParser.ClassDclContext classDclCtx : modelDclCtx.classDcl()) { String classFqn = classDclCtx.name.getText(); Class newClass = getOrAddClass(classFqn); // parents if (classDclCtx.parentDcl() != null) { final Class parentClass = getOrAddClass(classDclCtx.parentDcl().IDENT().getText()); newClass.setParent(parentClass); } // annotations for (GreyCatModelParser.AnnotationDclContext annotationDcl : classDclCtx.annotationDcl()) { addAnnotation(newClass, annotationDcl); } // attributes for (GreyCatModelParser.AttributeDclContext attDcl : classDclCtx.attributeDcl()) { addAttribute(newClass, attDcl); } // relations for (GreyCatModelParser.RelationDclContext relDclCtx : classDclCtx.relationDcl()) { addRelation(newClass, relDclCtx); } // references for (GreyCatModelParser.ReferenceDclContext refDclCtx : classDclCtx.referenceDcl()) { addReference(newClass, refDclCtx); } // local indexes for (GreyCatModelParser.LocalIndexDclContext localIndexDclCtx : classDclCtx.localIndexDcl()) { addLocalIndex(newClass, localIndexDclCtx); } // local constants for (GreyCatModelParser.ConstDclContext constDclCtx : classDclCtx.constDcl()) { addLocalConstant(newClass, constDclCtx); } } // opposite management for (GreyCatModelParser.ClassDclContext classDclCtx : modelDclCtx.classDcl()) { String classFqn = classDclCtx.name.getText(); Class classType = classes.get(classFqn); // relations for (GreyCatModelParser.RelationDclContext relDclCtx : classDclCtx.relationDcl()) { linkOppositeRelations(classType, relDclCtx); } // references for (GreyCatModelParser.ReferenceDclContext refDclCtx : classDclCtx.referenceDcl()) { linkOppositeReferences(classType, refDclCtx); } // local indexes for (GreyCatModelParser.LocalIndexDclContext idxDclCtx : classDclCtx.localIndexDcl()) { linkOppositeLocalIndexes(classType, idxDclCtx); } } // global indexes for (GreyCatModelParser.GlobalIndexDclContext globalIdxDclContext : modelDclCtx.globalIndexDcl()) { final String name = globalIdxDclContext.name.getText(); final String type = globalIdxDclContext.type.getText(); final Index index = getOrAddGlobalIndex(name, type); final Class indexedClass = getOrAddClass(index.type()); for (TerminalNode idxDclIdent : globalIdxDclContext.indexAttributesDcl().IDENT()) { index.addAttributeRef(new AttributeRef(indexedClass.getOrCreateAttribute(idxDclIdent.getText()))); } } // custom types for (GreyCatModelParser.CustomTypeDclContext customTypeDclCtx : modelDclCtx.customTypeDcl()) { String customTypeName = customTypeDclCtx.name.getText(); final CustomType newCustomType = getOrAddCustomType(customTypeName); // parents if (customTypeDclCtx.parentDcl() != null) { final CustomType parentCustomType = getOrAddCustomType( customTypeDclCtx.parentDcl().IDENT().getText()); newCustomType.setParent(parentCustomType); } // attributes for (GreyCatModelParser.AttributeDclContext attDcl : customTypeDclCtx.attributeDcl()) { addAttribute(newCustomType, attDcl); } // // relations // for (GreyCatModelParser.RelationDclContext relDcl : customTypeDclCtx.relationDcl()) { // addRelation(newCustomType, relDcl); // } // // references // for (GreyCatModelParser.ReferenceDclContext refDcl : customTypeDclCtx.referenceDcl()) { // addReference(newCustomType, refDcl); // } // // local indexes // for (GreyCatModelParser.LocalIndexDclContext localIndexDcl : customTypeDclCtx.localIndexDcl()) { // addLocalIndex(newCustomType, localIndexDcl); // } // local constants for (GreyCatModelParser.ConstDclContext constDclCtx : customTypeDclCtx.constDcl()) { addLocalConstant(newCustomType, constDclCtx); } // // opposite management // for (GreyCatModelParser.CustomTypeDclContext typeDclCtx : modelDclCtx.customTypeDcl()) { // String typeFqn = typeDclCtx.name.getText(); // CustomType customType = customTypes.get(typeFqn); // // // relations // for (GreyCatModelParser.RelationDclContext relDclCtx : typeDclCtx.relationDcl()) { // linkOppositeRelations(customType, relDclCtx); // } // // references // for (GreyCatModelParser.ReferenceDclContext refDclCtx : typeDclCtx.referenceDcl()) { // linkOppositeReferences(customType, refDclCtx); // } // // local indexes // for (GreyCatModelParser.LocalIndexDclContext idxDclCtx : typeDclCtx.localIndexDcl()) { // linkOppositeLocalIndexes(customType, idxDclCtx); // } // } } }
From source file:io.cloudchaser.murmur.MurmurASTVisitor.java
License:Open Source License
/** * Fetches a string with the original formatting of a context. * //from www . ja va 2s . co m * @param rule The rule to fetch the String for. * @return A String of a rule, as it appears in the source. */ private static String getOriginalText(ParserRuleContext rule) { int start = rule.start.getStartIndex(); int stop = rule.stop.getStopIndex(); Interval interval = new Interval(start, stop); return rule.start.getInputStream().getText(interval); }