use of org.antlr.v4.runtime.tree.ParseTree in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseNativeCallExpression.
protected NativeCallExpression parseNativeCallExpression(NativeCallContext ncc) {
NativeCallExpression nativeExp = null;
List<ParseTree> list = ncc.children;
// nativeCall: nativeVarRefChain (nativeMethod|nativeArray| PERIOD nativeVarRefChain)*;
NativeVarRefChainContext first = (NativeVarRefChainContext) list.get(0);
List<TerminalNode> ids = first.Identifier();
StringBuilder clsSb = new StringBuilder();
// 是类静态调用还是实例调用
boolean isCls = false;
int i = 0;
for (; i < ids.size(); i++) {
String text = ids.get(i).getText();
char c = text.charAt(0);
if (c >= 'A' && c <= 'Z') {
clsSb.append(text);
isCls = true;
break;
} else {
clsSb.append(text).append(".");
}
}
ClassNode clsNode = null;
InstanceNode insNode = null;
if (isCls) {
clsNode = new ClassNode(clsSb.toString());
// 指向下一个属性或者静态方法
i++;
} else {
// 变量的属性引用,回到第一个,构造一个变量
String varName = ids.get(0).getText();
VarRef ref = new VarRef(new VarAttribute[0], false, null, this.getBTToken(varName, ncc.start.getLine()));
this.pbCtx.setVarPosition(varName, ref);
insNode = new InstanceNode(ref);
i = 1;
}
List<NativeNode> nativeList = new ArrayList<NativeNode>();
for (int j = i; j < ids.size(); j++) {
// 剩下的是属性
NativeAtrributeNode attribute = new NativeAtrributeNode(ids.get(j).getText());
nativeList.add(attribute);
}
for (int z = 1; z < list.size(); z++) {
ParseTree tree = list.get(z);
if (tree instanceof NativeMethodContext) {
NativeMethodContext methodCtx = (NativeMethodContext) tree;
NativeMethodNode methodNode = null;
String method = null;
NativeNode lastNode = nativeList.get(nativeList.size() - 1);
if (lastNode instanceof NativeAtrributeNode) {
method = ((NativeAtrributeNode) lastNode).attribute;
//
nativeList.remove(nativeList.size() - 1);
} else {
String msg = null;
if (lastNode instanceof NativeArrayNode) {
msg = "[]()";
} else {
msg = "()()";
}
BeetlException ex = new BeetlException(BeetlException.PARSER_NATIVE_ERROR, msg);
ex.pushToken(this.getBTToken(methodCtx.getStart()));
throw ex;
}
// 解析参数
List<ExpressionContext> expCtxList = methodCtx.expression();
Expression[] exp = this.parseExpressionCtxList(expCtxList);
methodNode = new NativeMethodNode(method, exp);
nativeList.add(methodNode);
} else if (tree instanceof NativeArrayContext) {
ExpressionContext expCtx = ((NativeArrayContext) tree).expression();
Expression exp = this.parseExpress(expCtx);
NativeArrayNode arrayNode = new NativeArrayNode(exp);
nativeList.add(arrayNode);
} else if (tree instanceof NativeVarRefChainContext) {
List<TerminalNode> nodes = ((NativeVarRefChainContext) tree).Identifier();
for (TerminalNode node : nodes) {
NativeAtrributeNode attributeNode = new NativeAtrributeNode(node.getText());
nativeList.add(attributeNode);
}
} else {
// 其他节点,这段语法写的不好,造成解析困难,但先这样了
continue;
}
}
NativeNode[] chain = nativeList.toArray(new NativeNode[0]);
if (clsNode != null) {
nativeExp = new NativeCallExpression(clsNode, chain, this.getBTToken(ncc.start));
} else {
nativeExp = new NativeCallExpression(insNode, chain, this.getBTToken(ncc.start));
}
return nativeExp;
}
use of org.antlr.v4.runtime.tree.ParseTree in project vertexium by visallo.
the class CypherCstToAstVisitor method visitExpression4.
// + - prefix
@Override
public CypherAstBase visitExpression4(CypherParser.Expression4Context ctx) {
int neg = 0;
for (ParseTree child : ctx.children) {
if (child instanceof TerminalNode && child.getText().equals("-")) {
neg++;
}
}
CypherAstBase expr = visitExpression3(ctx.expression3());
if (neg % 2 == 1) {
return new CypherNegateExpression(expr);
} else {
return expr;
}
}
use of org.antlr.v4.runtime.tree.ParseTree in project vertexium by visallo.
the class CypherCstToAstVisitor method toBinaryExpressions.
private <T extends ParseTree> CypherBinaryExpression toBinaryExpressions(List<ParseTree> children, Function<T, CypherAstBase> itemTransform) {
CypherAstBase left = null;
CypherBinaryExpression.Op op = null;
for (int i = 0; i < children.size(); i++) {
ParseTree child = children.get(i);
if (child instanceof TerminalNode) {
CypherBinaryExpression.Op newOp = CypherBinaryExpression.Op.parseOrNull(child.getText());
if (newOp != null) {
if (op == null) {
op = newOp;
} else {
throw new VertexiumException("unexpected op, found too many ops in a row");
}
}
} else {
// noinspection unchecked
CypherAstBase childObj = itemTransform.apply((T) child);
if (left == null) {
left = childObj;
} else {
if (op == null) {
throw new VertexiumException("unexpected binary expression. expected an op between expressions");
}
left = new CypherBinaryExpression(left, op, childObj);
}
op = null;
}
}
return (CypherBinaryExpression) left;
}
use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.
the class LoggingTreeSubPage method addPluginToList.
/**
* Adds a new logging plugin to the list of logging plugins available
* for a given component. If necessary also creates the list of logging
* plugins.
*
* @param componentName
* the name of the component to add the plugin to.
* @param pluginName
* the name of the plugin to add
* @param path
* the path of the plugin to add, or null if none.
*/
private void addPluginToList(final String componentName, final String pluginName, final String path) {
if (loggingSectionHandler == null) {
return;
}
final StringBuilder pluginBuilder = new StringBuilder();
pluginBuilder.append(pluginName);
if (path != null && path.length() != 0) {
pluginBuilder.append(" := \"").append(path).append('\"');
}
/*
* loggingSectionHandler.getLastSectionRoot()
* entry.getLoggerPluginsRoot()
* entry.getLoggerPluginsListRoot()
* {
* pluginEntry.getLoggerPluginRoot()
* }
*/
LoggingSectionHandler.LoggerPluginsEntry entry = loggingSectionHandler.getLoggerPluginsTree().get(componentName);
if (entry == null) {
entry = new LoggingSectionHandler.LoggerPluginsEntry();
loggingSectionHandler.getLoggerPluginsTree().put(componentName, entry);
final ParseTree loggerPluginsRoot = new ParserRuleContext();
ConfigTreeNodeUtilities.addChild(loggingSectionHandler.getLastSectionRoot(), loggerPluginsRoot);
entry.setLoggerPluginsRoot(loggerPluginsRoot);
final StringBuilder builder = new StringBuilder();
builder.append('\n').append(componentName).append(".LoggerPlugins := ");
ConfigTreeNodeUtilities.addChild(loggerPluginsRoot, new AddedParseTree(builder.toString()));
ConfigTreeNodeUtilities.addChild(loggerPluginsRoot, new AddedParseTree("{"));
final ParseTree loggerPluginsListRoot = new ParserRuleContext();
entry.setLoggerPluginsListRoot(loggerPluginsListRoot);
final LoggingSectionHandler.LoggerPluginEntry pluginEntry = new LoggingSectionHandler.LoggerPluginEntry();
final ParseTree pluginRoot = new ParserRuleContext();
pluginEntry.setLoggerPluginRoot(pluginRoot);
pluginEntry.setName(pluginName);
pluginEntry.setPath(path);
ConfigTreeNodeUtilities.addChild(pluginRoot, new AddedParseTree(pluginBuilder.toString()));
entry.setPluginRoots(new HashMap<String, LoggingSectionHandler.LoggerPluginEntry>(1));
entry.getPluginRoots().put(pluginName, pluginEntry);
ConfigTreeNodeUtilities.addChild(loggerPluginsListRoot, pluginRoot);
ConfigTreeNodeUtilities.addChild(loggerPluginsRoot, loggerPluginsListRoot);
ConfigTreeNodeUtilities.addChild(loggerPluginsRoot, new AddedParseTree("}"));
return;
}
final int childCount = entry.getLoggerPluginsListRoot().getChildCount();
ConfigTreeNodeUtilities.addChild(entry.getLoggerPluginsListRoot(), new AddedParseTree((childCount > 0 ? ", " : "") + pluginBuilder.toString()));
}
use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.
the class ModuleParameterSectionDropTargetListener method drop.
@Override
public void drop(final DropTargetEvent event) {
if (ModuleParameterTransfer.getInstance().isSupportedType(event.currentDataType)) {
if (event.item != null && viewer.getInput() != null) {
ModuleParameterSectionHandler moduleParameterSectionHandler = (ModuleParameterSectionHandler) viewer.getInput();
ModuleParameter element = (ModuleParameter) event.item.getData();
ModuleParameter[] items = (ModuleParameter[]) event.data;
int baseindex = moduleParameterSectionHandler.getModuleParameters().indexOf(element);
final ParseTree parent = moduleParameterSectionHandler.getLastSectionRoot();
ConfigTreeNodeUtilities.removeChild(parent, element.getRoot());
ConfigTreeNodeUtilities.addChild(parent, element.getRoot(), baseindex);
if (items.length > 0) {
for (int i = 0; i < items.length - 1; i++) {
moduleParameterSectionHandler.getModuleParameters().add(++baseindex, items[i]);
}
moduleParameterSectionHandler.getModuleParameters().add(++baseindex, items[items.length - 1]);
}
viewer.refresh(true);
editor.setDirty();
}
}
}
Aggregations