summaryrefslogtreecommitdiff
path: root/compiler/src/main/java/android/databinding/tool/ExpressionParser.java
blob: 8deebc4d9e165c8120570f557034fbb1e18b4335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.databinding.tool;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.ParseTreeListener;
import org.antlr.v4.runtime.tree.TerminalNode;

import android.databinding.parser.BindingExpressionLexer;
import android.databinding.parser.BindingExpressionParser;
import android.databinding.tool.expr.Expr;
import android.databinding.tool.expr.ExprModel;
import android.databinding.tool.processing.ErrorMessages;
import android.databinding.tool.store.Location;
import android.databinding.tool.util.L;
import android.databinding.tool.util.Preconditions;

import com.android.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class ExpressionParser {
    final ExprModel mModel;
    final ExpressionVisitor visitor;

    public ExpressionParser(ExprModel model) {
        mModel = model;
        visitor = new ExpressionVisitor(mModel);
    }

    public Expr parse(String input, @Nullable Location locationInFile) {
        ANTLRInputStream inputStream = new ANTLRInputStream(input);
        BindingExpressionLexer lexer = new BindingExpressionLexer(inputStream);
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        final BindingExpressionParser parser = new BindingExpressionParser(tokenStream);
        parser.addErrorListener(new BaseErrorListener() {
            @Override
            public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                    int charPositionInLine, String msg, RecognitionException e) {
                L.e(ErrorMessages.SYNTAX_ERROR, msg);
            }
        });
        BindingExpressionParser.BindingSyntaxContext root = parser.bindingSyntax();
        try {
            mModel.setCurrentLocationInFile(locationInFile);
            visitor.setParseTreeListener(new ParseTreeListener() {
                List<ParserRuleContext> mStack = new ArrayList<ParserRuleContext>();
                @Override
                public void visitTerminal(TerminalNode node) {
                }

                @Override
                public void visitErrorNode(ErrorNode node) {
                }

                @Override
                public void enterEveryRule(ParserRuleContext ctx) {
                    mStack.add(ctx);
                    mModel.setCurrentParserContext(ctx);
                }

                @Override
                public void exitEveryRule(ParserRuleContext ctx) {
                    Preconditions.check(ctx == mStack.get(mStack.size() - 1),
                            "Inconsistent exit from context. Received %s, expecting %s",
                            ctx.toInfoString(parser),
                            mStack.get(mStack.size() - 1).toInfoString(parser));
                    mStack.remove(mStack.size() - 1);
                    if (mStack.size() > 0) {
                        mModel.setCurrentParserContext(mStack.get(mStack.size() - 1));
                    } else {
                        mModel.setCurrentParserContext(null);
                    }
                }
            });
            return root.accept(visitor);
        } finally {
            mModel.setCurrentLocationInFile(null);
        }
    }

    public ExprModel getModel() {
        return mModel;
    }
}