blob: 63f05e4b03494c149308bb7c1a42bc40abeaad9a [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.skywalking.oal.tool.parser;
import java.util.List;
import org.antlr.v4.runtime.misc.NotNull;
import org.apache.skywalking.oal.tool.grammar.OALParser;
import org.apache.skywalking.oal.tool.grammar.OALParserBaseListener;
public class OALListener extends OALParserBaseListener {
private List<AnalysisResult> results;
private AnalysisResult current;
private ConditionExpression conditionExpression;
public OALListener(List<AnalysisResult> results) {
this.results = results;
}
@Override
public void enterAggregationStatement(@NotNull OALParser.AggregationStatementContext ctx) {
current = new AnalysisResult();
}
@Override
public void exitAggregationStatement(@NotNull OALParser.AggregationStatementContext ctx) {
DeepAnalysis deepAnalysis = new DeepAnalysis();
results.add(deepAnalysis.analysis(current));
current = null;
}
@Override public void enterSource(OALParser.SourceContext ctx) {
current.setSourceName(ctx.getText());
}
@Override
public void enterSourceAttribute(OALParser.SourceAttributeContext ctx) {
current.setSourceAttribute(ctx.getText());
}
@Override public void enterVariable(OALParser.VariableContext ctx) {
}
@Override public void exitVariable(OALParser.VariableContext ctx) {
current.setMetricName(metricNameFormat(ctx.getText()));
current.setTableName(ctx.getText().toLowerCase());
}
@Override public void enterFunctionName(OALParser.FunctionNameContext ctx) {
current.setAggregationFunctionName(ctx.getText());
}
@Override public void enterConditionAttribute(OALParser.ConditionAttributeContext ctx) {
conditionExpression.setAttribute(ctx.getText());
}
@Override public void enterBooleanBinaryMatch(OALParser.BooleanBinaryMatchContext ctx) {
conditionExpression.setExpressionType("booleanMatch");
}
@Override public void enterConditionValue(OALParser.ConditionValueContext ctx) {
conditionExpression.setValue(ctx.getText());
}
@Override public void enterFuncParamExpression(OALParser.FuncParamExpressionContext ctx) {
conditionExpression = new ConditionExpression();
}
@Override public void exitFuncParamExpression(OALParser.FuncParamExpressionContext ctx) {
current.addFuncConditionExpression(conditionExpression);
}
@Override public void exitBooleanBinaryMatch(OALParser.BooleanBinaryMatchContext ctx) {
}
private String metricNameFormat(String source) {
source = firstLetterUpper(source);
int idx;
while ((idx = source.indexOf("_")) > -1) {
source = source.substring(0, idx) + firstLetterUpper(source.substring(idx + 1));
}
return source;
}
private String firstLetterUpper(String source) {
return source.substring(0, 1).toUpperCase() + source.substring(1);
}
}