public static void main(String[] argv) { /* Node root = new Node(); root.setUserObject("root"); Node r = new Node(); r.setUserObject("r"); Node q = new Node(); q.setUserObject("q"); root.addNode(q); root.addNode(r); Node a = new Node(); a.setUserObject("a"); q.addNode(a); // System.out.println(root.deepTrans(root)); // System.out.println(root.deepTrans(root.clone())); Node x = new Node(); x.setUserObject("x"); Node y = new Node(); y.setUserObject("y"); Node z = new Node(); z.setUserObject("z"); x.addNode(y); x.addNode(z); System.out.println(x.deepTrans(x.clone())); */ ExpressionParser p = new ExpressionParser(); Node<Token> n = p.parse("(a or b) and c or d and f"); System.out.println(n); System.out.println(ExpressionParser.getExpressionString(n)); }
@Override public Object excute(String id, String expression) { try { ExpressionParser parser = new ExpressionParser(id, expression); Node rootNode = parser.parser(); rootNode.execute(mPage); return rootNode.getValue(); } catch (Exception e) { e.printStackTrace(); return null; } }
@Override public Expression toExpression(List<Symbol> symbols) throws ParserSymbolHandleException, VariableCycleException { this.symbols = symbols; this.symbols.add(new EndSymbol()); ExpressionParser ep = new ExpressionParser(this); returnExpression = ep.toExpression(symbols); symbols.get(0).accept(new EndSymbolVisitor()); return returnExpression; }
public static void main(String[] args) { // 创建一个ExpressionParser对象,用于解析表达式 ExpressionParser parser = new SpelExpressionParser(); List<String> list = new ArrayList<String>(); list.add("疯狂Java讲义"); list.add("疯狂Ajax讲义"); list.add("疯狂XML讲义"); list.add("经典Java EE企业应用实战"); EvaluationContext ctx = new StandardEvaluationContext(); ctx.setVariable("mylist", list); // 得到的新集合的元素是原集合的每个元素length()方法返回值 Expression expr = parser.parseExpression("#mylist.![length()]"); System.out.println(expr.getValue(ctx)); List<Person> list2 = new ArrayList<Person>(); list2.add(new Person(1, "孙悟空", 162)); list2.add(new Person(1, "猪八戒", 182)); list2.add(new Person(1, "牛魔王", 195)); ctx.setVariable("mylist2", list2); // 得到的新集合的元素是原集合的每个元素name属性值 expr = parser.parseExpression("#mylist2.![name]"); System.out.println(expr.getValue(ctx)); }
@Override public LogQueryCommand parse(LogQueryContext context, String commandString) { // find assignment symbol int p = QueryTokenizer.findKeyword(commandString, "="); if (p < 0) throw new LogQueryParseException("assign-token-not-found", commandString.length()); String field = commandString.substring(COMMAND.length(), p).trim(); String exprToken = commandString.substring(p + 1).trim(); if (field.isEmpty()) throw new LogQueryParseException("field-name-not-found", commandString.length()); if (exprToken.isEmpty()) throw new LogQueryParseException("expression-not-found", commandString.length()); Expression expr = ExpressionParser.parse(exprToken); return new Eval(field, expr); }
/** * Parse a Triangle SingleDeclarationParser. * * <p>Single-Declaration ::= const Identifier ~ Expression | var Identifier : Type-Denoter | proc * Identifier (Formal-Parameter-Sequence) ~ Single-Command | func Identifier * (Formal-Parameter-Sequence) : Type-Denoter ~ Expression | Type Identifier ~ Type-Denoter * * <p>To be overridden by the specialized command parse subclasses. * * @param token the initial token. * @return the root node of the generated parse tree. * @throws Exception if an error occurred. */ public void parse(Token token) throws Exception { SingleCommandParser singleCommand = null; ExpressionParser expression = null; FormalParameterSequenceParser formalParameterSequence = null; TypeDenoterParser typeDenoter = null; EnumSet<TriangleTokenType> syncSet = null; TypeSpec identifierType = TrianglePredefined.undefinedType; Token identifierToken = null; ICode routineICode = null; token = currentToken(); switch ((TriangleTokenType) token.getType()) { case CONST: identifierToken = nextToken(); syncSet = EnumSet.of(TILDE); syncSet.addAll(ExpressionParser.FIRST_FOLLOW_SET); synchronize(IDENTIFIER, MISSING_IDENTIFIER, syncSet); syncSet.remove(TILDE); token = synchronize(TILDE, MISSING_TILDE, syncSet); expression = new ExpressionParser(this); ICodeNode expressionNode = expression.parse(token); SymTabEntry constantId = symTabStack.lookupLocal(identifierToken.getText().toLowerCase()); // Enter the new identifier into the symbol table // but don't set how it's defined yet. if (constantId == null) { constantId = symTabStack.enterLocal(identifierToken.getText().toLowerCase()); constantId.setDefinition(DefinitionImpl.CONSTANT); constantId.setAttribute(CONSTANT_VALUE, expressionNode); constantId.appendLineNumber(identifierToken.getLineNumber()); constantId.setTypeSpec(expressionNode.getTypeSpec()); } else { errorHandler.flag(identifierToken, IDENTIFIER_REDEFINED, this); } break; case VAR: identifierToken = nextToken(); syncSet = EnumSet.of(COLON); syncSet.addAll(TypeDenoterParser.FIRST_FOLLOW_SET); synchronize(IDENTIFIER, MISSING_IDENTIFIER, syncSet); syncSet.remove(COLON); token = synchronize(COLON, MISSING_COLON, syncSet); typeDenoter = new TypeDenoterParser(this); identifierType = typeDenoter.parse(token); SymTabEntry variableId = symTabStack.lookupLocal(identifierToken.getText().toLowerCase()); // Enter the new identifier into the symbol table // but don't set how it's defined yet. if (variableId == null) { variableId = symTabStack.enterLocal(identifierToken.getText().toLowerCase()); variableId.setDefinition(DefinitionImpl.VARIABLE); variableId.appendLineNumber(identifierToken.getLineNumber()); variableId.setTypeSpec(identifierType); } else { errorHandler.flag(identifierToken, IDENTIFIER_REDEFINED, this); } break; case PROC: identifierToken = nextToken(); routineICode = ICodeFactory.createICode(); syncSet = EnumSet.of(LEFT_PAREN); syncSet.addAll(FormalParameterSequenceParser.FIRST_FOLLOW_SET); token = synchronize(IDENTIFIER, MISSING_IDENTIFIER, syncSet); syncSet.remove(LEFT_PAREN); token = synchronize(LEFT_PAREN, MISSING_LEFT_PAREN, syncSet); SymTabEntry procId = symTabStack.lookupLocal(identifierToken.getText().toLowerCase()); // Enter the new identifier into the symbol table // but don't set how it's defined yet. if (procId == null) { procId = symTabStack.enterLocal(identifierToken.getText().toLowerCase()); procId.setTypeSpec(TrianglePredefined.undefinedType); procId.setDefinition(DefinitionImpl.PROCEDURE); procId.appendLineNumber(identifierToken.getLineNumber()); procId.setAttribute(ROUTINE_SYMTAB, symTabStack.push()); procId.setAttribute(ROUTINE_ICODE, routineICode); } else { errorHandler.flag(identifierToken, IDENTIFIER_REDEFINED, this); procId = null; } formalParameterSequence = new FormalParameterSequenceParser(this); ArrayList<SymTabEntry> procParamList = formalParameterSequence.parse(token); syncSet = EnumSet.of(TILDE); syncSet.addAll(SingleCommandParser.FIRST_FOLLOW_SET); token = synchronize(RIGHT_PAREN, MISSING_RIGHT_PAREN, syncSet); syncSet.remove(TILDE); token = synchronize(TILDE, MISSING_TILDE, syncSet); singleCommand = new SingleCommandParser(this); routineICode.setRoot(singleCommand.parse(token)); if (procId != null) { procId.setAttribute(ROUTINE_PARMS, procParamList); } symTabStack.pop(); break; case FUNC: identifierToken = nextToken(); routineICode = ICodeFactory.createICode(); SymTabEntry funcId = symTabStack.lookupLocal(identifierToken.getText().toLowerCase()); // Enter the new identifier into the symbol table // but don't set how it's defined yet. if (funcId == null) { funcId = symTabStack.enterLocal(identifierToken.getText().toLowerCase()); funcId.setDefinition(DefinitionImpl.FUNCTION); funcId.appendLineNumber(identifierToken.getLineNumber()); funcId.setTypeSpec(TrianglePredefined.undefinedType); funcId.setAttribute(ROUTINE_SYMTAB, symTabStack.push()); funcId.setAttribute(ROUTINE_ICODE, routineICode); } else { errorHandler.flag(identifierToken, IDENTIFIER_REDEFINED, this); funcId = null; } syncSet = EnumSet.of(LEFT_PAREN); syncSet.addAll(FormalParameterSequenceParser.FIRST_FOLLOW_SET); token = synchronize(IDENTIFIER, MISSING_IDENTIFIER, syncSet); syncSet.remove(LEFT_PAREN); token = synchronize(LEFT_PAREN, MISSING_LEFT_PAREN, syncSet); formalParameterSequence = new FormalParameterSequenceParser(this); ArrayList<SymTabEntry> funcParamList = formalParameterSequence.parse(token); syncSet = EnumSet.of(COLON); syncSet.addAll(TypeDenoterParser.FIRST_FOLLOW_SET); synchronize(RIGHT_PAREN, MISSING_RIGHT_PAREN, syncSet); syncSet.remove(COLON); token = synchronize(COLON, MISSING_COLON, syncSet); Token typeToken = currentToken(); typeDenoter = new TypeDenoterParser(this); TypeSpec funcType = typeDenoter.parse(token); syncSet = ExpressionParser.FIRST_FOLLOW_SET.clone(); token = synchronize(TILDE, MISSING_TILDE, syncSet); expression = new ExpressionParser(this); routineICode.setRoot(expression.parse(token)); if (funcId != null) { funcId.setTypeSpec(funcType); funcId.setAttribute(ROUTINE_PARMS, funcParamList); if (!routineICode.getRoot().getTypeSpec().equals(funcType)) { errorHandler.flag(typeToken, RETURN_TYPE_MISMATCH, this); } } symTabStack.pop(); break; case TYPE: identifierToken = nextToken(); syncSet = EnumSet.of(TILDE); syncSet.addAll(TypeDenoterParser.FIRST_FOLLOW_SET); synchronize(IDENTIFIER, MISSING_IDENTIFIER, syncSet); syncSet.remove(TILDE); token = synchronize(TILDE, MISSING_TILDE, syncSet); typeDenoter = new TypeDenoterParser(this); TypeSpec typeType = typeDenoter.parse(token); SymTabEntry typeId = symTabStack.lookupLocal(identifierToken.getText().toLowerCase()); // Enter the new identifier into the symbol table // but don't set how it's defined yet. if (typeId == null) { typeId = symTabStack.enterLocal(identifierToken.getText().toLowerCase()); typeId.setDefinition(DefinitionImpl.TYPE); typeId.appendLineNumber(identifierToken.getLineNumber()); typeId.setTypeSpec(typeType); } else { errorHandler.flag(token, IDENTIFIER_REDEFINED, this); } break; default: errorHandler.flag(token, MISSING_DECLARATION, this); break; } }
public static ExpressionItem parse(String value) throws Exception { ExpressionParser parser = new ExpressionParser(convert(value)); ExpressionItem item = parser.parse(); return item; }
public static List<ExpressionItem> parseArithmeticExpressions(String value) throws Exception { List<ExpressionItem> items; ExpressionParser parser = new ExpressionParser(convert(value)); items = parser.parseArithmeticExpressions(); return items; }
/** * 解析IK简易查询表达式 * * @param ikQueryExp * @return Query 查询逻辑对象 */ public static Query parse(String ikQueryExp) { ExpressionParser ikExpParser = new ExpressionParser(); return ikExpParser.parserExp(ikQueryExp); }
public Object eval(Map<String, Object> env) { return parent.getCurrentDate(); }