1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
;
5 import com
.google
.apphosting
.api
.AppEngineInternal
;
7 import org
.antlr
.runtime
.ANTLRStringStream
;
8 import org
.antlr
.runtime
.RecognitionException
;
9 import org
.antlr
.runtime
.TokenRewriteStream
;
10 import org
.antlr
.runtime
.tree
.CommonTree
;
11 import org
.antlr
.runtime
.tree
.CommonTreeAdaptor
;
14 * A generator of AST representation of an expression. This class can use an
15 * optionally supplied CommonTreeAdaptor to process the tree further.
16 * If successful it returns the root of an AST representing the parsed query.
20 public class ExpressionTreeBuilder
{
21 private final CommonTreeAdaptor adaptor
;
23 public ExpressionTreeBuilder() {
24 this(new CommonTreeAdaptor());
27 public ExpressionTreeBuilder(CommonTreeAdaptor adaptor
) {
28 this.adaptor
= adaptor
;
32 * Parses the user expression and returns a {@link CommonTree}.
34 * @param expression the expression to parse
35 * @return a CommonTree constructed from the expression
36 * @throws RecognitionException if the user expression is invalid
38 public CommonTree
parse(String expression
) throws RecognitionException
{
39 ANTLRStringStream stream
= new ANTLRStringStream(expression
);
40 ExpressionLexer lexer
= new ExpressionLexer(stream
);
41 TokenRewriteStream tokens
= new TokenRewriteStream(lexer
);
42 ExpressionParser parser
= new ExpressionParser(tokens
);
43 parser
.setTreeAdaptor(adaptor
);
44 return (CommonTree
) parser
.expression().getTree();