/** Creates a parser for a given term, generating trees of this kind. */ public <P extends Parser> P createParser(Class<P> parserType, I info, String term) { try { // find the lexer type String parserName = parserType.getName(); String lexerName = parserName.substring(0, parserName.indexOf("Parser")) .concat("Lexer"); @SuppressWarnings("unchecked") Class<? extends Lexer> lexerType = (Class<? extends Lexer>) Class.forName(lexerName); Lexer lexer = createLexer(lexerType, info, term); // instantiate the parser CommonTokenStream tokenStream = new CommonTokenStream(lexer); Constructor<P> parserConstructor = parserType.getConstructor(TokenStream.class); P result = parserConstructor.newInstance(tokenStream); Method adaptorSetter = parserType.getMethod("setTreeAdaptor", TreeAdaptor.class); adaptorSetter.invoke(result, new ParseTreeAdaptor<>(this, info, tokenStream)); callInitialise(result, info); return result; } catch (Exception e) { throw toRuntime(e); } }
/** * Add a new attribute to a parent node. * @param adaptor the tree helper * @param parent parent node * @param tokenType the new node token type * @param tokenText the new node name * @param value the new node value * @return a new node that was just added to parent */ private Object addAttribute( final TreeAdaptor adaptor, final Object parent, final int tokenType, final String tokenText, final String value) { Object node = adaptor.create(tokenType, tokenText); if (value != null) { String[] values = value.split("\\s\\|\\s"); for (String avalue : values) { Object nodeValue = adaptor.create(CobolStructureParser.LITERAL, avalue); adaptor.addChild(node, nodeValue); } } adaptor.addChild(parent, node); return node; }
public Object buildJDBCUrl(TreeAdaptor adaptor, String dbType, String hostname, String port, String dbname) { StringBuilder buf = new StringBuilder(); buf.append("'"); buf.append("jdbc:"); if (dbType == null) throw new ParserException(Pass.FIRST, "Malformed jdbc url: missing dbtype"); buf.append(dbType).append("://"); if (hostname == null) throw new ParserException(Pass.FIRST, "Malformed jdbc url: missing host"); buf.append(hostname); if (port != null) buf.append(":").append(port); if (dbname != null) buf.append("/").append(dbname); buf.append("'"); return adaptor.create(TokenTypes.Character_String_Literal, buf.toString()); }
public ExpressionNode buildBooleanExpr(TreeAdaptor ta, Token ln, ExpressionNode predicate, Token is, Token not, List<ExpressionNode> is_parameters) { ExpressionNode out = predicate; if (is != null) { BaseTreeAdaptor bta = (BaseTreeAdaptor) ta; List<ExpressionNode> params = new ArrayList<ExpressionNode>(); params.add(predicate); Token ftok = is; if (not != null) { int newType = findNottableConversion(ftok); ftok = bta.createToken(newType,ftok.getText()); } params.addAll(is_parameters); out = new FunctionCall(new FunctionName(ftok.getText(), ftok.getType(), true),params); } if (ln != null) { out = new FunctionCall(new FunctionName(ln.getText(), ln.getType(), true), out); } return out; }
/** Creates a tree parser for a given tree of this kind. */ public <P extends TreeParser> P createTreeParser(Class<P> parserType, I info) { try { // instantiate the parser ParseTreeAdaptor<T,I> adaptor = new ParseTreeAdaptor<>(this); Constructor<P> parserConstructor = parserType.getConstructor(TreeNodeStream.class); P result = parserConstructor.newInstance(adaptor.createTreeNodeStream(this)); Method adaptorSetter = parserType.getMethod("setTreeAdaptor", TreeAdaptor.class); adaptorSetter.invoke(result, adaptor); callInitialise(result, info); return result; } catch (Exception e) { throw toRuntime(e); } }
private TemplateGroupRuleReturnScope buildAstForGroupTemplates(TemplateGroupWrapper group) { TreeAdaptor adaptor = new CommonTreeAdaptor(); Object tree = adaptor.nil(); for (CompiledST template : group.getCompiledTemplates()) { adaptor.addChild(tree, template.ast); } return new TemplateGroupRuleReturnScope(group, (CommonTree)tree); }
public Object buildKeywordEscapeIdentifier(TreeAdaptor adaptor, Object tree) { if (tree instanceof CommonToken) { Token tok = (Token) tree; return adaptor.create(TokenTypes.Regular_Identifier, tok, tok.getText()); } else { CommonTree ct = (CommonTree)tree; return adaptor.create(TokenTypes.Regular_Identifier, ct.getToken(), ct.getText()); } }
public Object buildKeywordEscapeIdentifier(Parser parser, TreeAdaptor adaptor, Object tree) { CommonTree ct = (CommonTree)tree; String txt = ct.getText(); if (txt == null) { CommonToken leftToken = (CommonToken) parser.getTokenStream().get(ct.getTokenStartIndex()); CommonToken rightToken = (CommonToken) parser.getTokenStream().get(ct.getTokenStopIndex()); txt = leftToken.getInputStream().substring(leftToken.getStartIndex(), rightToken.getStopIndex()); } return adaptor.create(TokenTypes.Regular_Identifier, ct.getToken(), /*ct.getText()*/txt); }
private static ExpressionNode createPrecedenceTree(TranslatorUtils me, TreeAdaptor adaptor, List<ExpressionNode> expressions, List<Token> operators, int startIndex, int stopIndex) { if (stopIndex == startIndex) { ExpressionNode out = expressions.get(startIndex); if (out instanceof ExpressionNodeList) throw new IllegalStateException("unhandled fake expression"); return out; } int pivot = findPivot(operators, startIndex, stopIndex - 1); Token opToken = operators.get(pivot); FunctionName fn = new FunctionName(opToken.getText(),opToken.getType(),true); if (specialOperators.contains(opToken.getType())) { if (pivot+1 == stopIndex) { // unpack the rhs ExpressionNode en = expressions.get(stopIndex); ArrayList<ExpressionNode> allParams = new ArrayList<ExpressionNode>(); allParams.add(createPrecedenceTree(me,adaptor,expressions,operators,startIndex,pivot)); if (en instanceof ExpressionNodeList) { ExpressionNodeList enl = (ExpressionNodeList) en; allParams.addAll(enl.args); } else { allParams.add(en); } return me.buildFunctionCall(fn, allParams, null, null); } } return me.buildFunctionCall(fn, Arrays.asList(new ExpressionNode[] { createPrecedenceTree(me,adaptor,expressions,operators,startIndex,pivot), createPrecedenceTree(me,adaptor,expressions,operators,pivot+1,stopIndex) }), null, null); }
public void collectSpecialNottable(TreeAdaptor adaptor, Object mainOp, List<ExpressionNode> params, Object notOp, List<ExpressionNode> exprs, List<Token> opNodes) { BaseTreeAdaptor bta = (BaseTreeAdaptor) adaptor; CommonTree opNode = (CommonTree) mainOp; CommonTree notNode = (CommonTree) notOp; CommonToken opToken = null; if (notNode != null) { int newType = findNottableConversion(opNode); opToken = (CommonToken) bta.createToken(newType, opNode.getText()); } else { opToken = (CommonToken) opNode.getToken(); } opNodes.add(opToken); exprs.add(new ExpressionNodeList(params)); }
public FunctionName buildNottableFunction(TreeAdaptor adaptor, Token in) { BaseTreeAdaptor bta = (BaseTreeAdaptor) adaptor; int nt = findNottableConversion(in); Token newTok = bta.createToken(nt,in.getText()); TranslatorUtils me = (TranslatorUtils) this; return me.buildFunctionName(newTok, false); }
public Object execParser( String ruleName, String input, int scriptLine) throws Exception { ANTLRStringStream is = new ANTLRStringStream(input); Class<? extends TokenSource> lexerClass = Class.forName(lexerClassName).asSubclass(TokenSource.class); Constructor<? extends TokenSource> lexConstructor = lexerClass.getConstructor(CharStream.class); TokenSource lexer = lexConstructor.newInstance(is); is.setLine(scriptLine); CommonTokenStream tokens = new CommonTokenStream(lexer); Class<? extends Parser> parserClass = Class.forName(parserClassName).asSubclass(Parser.class); Constructor<? extends Parser> parConstructor = parserClass.getConstructor(TokenStream.class); Parser parser = parConstructor.newInstance(tokens); // set up customized tree adaptor if necessary if ( adaptorClassName!=null ) { Method m = parserClass.getMethod("setTreeAdaptor", TreeAdaptor.class); Class<? extends TreeAdaptor> adaptorClass = Class.forName(adaptorClassName).asSubclass(TreeAdaptor.class); m.invoke(parser, adaptorClass.newInstance()); } Method ruleMethod = parserClass.getMethod(ruleName); // INVOKE RULE return ruleMethod.invoke(parser); }
public void setTreeAdaptor(TreeAdaptor adaptor) { this.adaptor = adaptor; }
public TreeAdaptor getTreeAdaptor() { return adaptor; }
public JTreeASTModel(TreeAdaptor adaptor, Object root) { this.adaptor = adaptor; this.root = root; }
public void setTreeAdaptor(final TreeAdaptor adaptor) { this.adaptor = adaptor; }