List of usage examples for org.antlr.v4.runtime.tree ParseTreeProperty get
public V get(ParseTree node)
From source file:edu.clemson.cs.r2jt.misc.Utils.java
License:Open Source License
/** * Returns a list of {@code E} given: an expected type {@code T}, some * number/* ww w .j ava 2s .c om*/ * of concrete syntax {@code nodes}, and a mapping from rule contexts to * some number of elements descending from {@code E}. * * @param expectedType The class type to inhabit the returned list * @param nodes A list of concrete syntax nodes, as obtained through * a visitor, listener, etc. * @param annotations A map from rule context to the primary supertype * of {@code expectedType} ({@code E}). * @param <E> Super type of {@code expectedType}. * @param <T> The expected type. * @return A list of {@code T}. */ public static <E, T extends E> List<T> collect(Class<T> expectedType, List<? extends ParseTree> nodes, ParseTreeProperty<? extends E> annotations) { List<T> result = new ArrayList<>(); for (ParseTree node : nodes) { result.add(expectedType.cast(annotations.get(node))); } return result; }
From source file:edu.clemson.resolve.misc.Utils.java
License:BSD License
/** * Returns a list of {@code E} given: an expected type {@code T}, some number of concrete syntax {@code nodes}, * and a mapping from rule contexts to some number of elements descending from {@code E}. * * @param expectedType the class type to inhabit the returned list * @param nodes a list of concrete syntax nodes, as obtained through a visitor, listener, etc. * @param annotations a map from rule context to the primary supertype of {@code expectedType} ({@code E}). * @param <E> super type of {@code expectedType}. * @param <T> the expected type. * * @return a list of {@code T}.//from w ww . j a va 2 s . c om */ @NotNull public static <E, T extends E> List<T> collect(@NotNull Class<T> expectedType, @NotNull List<? extends ParseTree> nodes, @NotNull ParseTreeProperty<? extends E> annotations) { return nodes.stream().map(x -> expectedType.cast(annotations.get(x))).collect(Collectors.toList()); }
From source file:net.udidb.expr.lang.c.CExpressionCompiler.java
License:Open Source License
@VisibleForTesting Expression compile(String expression, ExecutionContext executionContext, boolean displayTree) throws ExpressionException { long pc = 0;//ww w. ja v a 2s . c o m Function currentFunction = null; if (!executionContext.getCurrentThread().getParentProcess().isWaitingForStart()) { try { pc = executionContext.getCurrentThread().getPC(); } catch (UdiException e) { throw new ExpressionException(e); } currentFunction = executionContext.getExecutable().getContainingFunction(pc); } final ParseTreeProperty<NodeState> states = new ParseTreeProperty<>(); // Parse the expression final CParser parser; try { parser = createParser(expression); } catch (IOException e) { throw new ExpressionException(e); } ParserRuleContext parseTree = parser.expression(); // Resolve all symbols, interrogating the debuggee as necessary resolveSymbols(parseTree, states, executionContext, currentFunction, pc); // Type checking typeCheckExpression(parseTree, states); // Simplify the expression given the current state of the AST simplifyExpression(parseTree, states, executionContext); // Generate code and produce Expression to encapsulate the result Expression output = generateCode(parseTree, states, executionContext); if (displayTree) { TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), parseTree); viewer.setTreeTextProvider(new TreeTextProvider() { TreeTextProvider defaultProvider = new DefaultTreeTextProvider( Arrays.asList(parser.getRuleNames())); @Override public String getText(Tree node) { if (node instanceof ParseTree) { NodeState nodeState = states.get((ParseTree) node); ExpressionValue value; if (nodeState != null) { value = nodeState.getExpressionValue(); } else { value = null; } if (value != null) { return defaultProvider.getText(node) + "(" + value + ")"; } else { return defaultProvider.getText(node) + "(null)"; } } return defaultProvider.getText(node); } }); viewer.open(); } return output; }