use of org.antlr.v4.runtime.Token in project incubator-systemml by apache.
the class CommonSyntacticValidator method unaryExpressionHelper.
protected void unaryExpressionHelper(ParserRuleContext ctx, ExpressionInfo left, ExpressionInfo me, String op) {
if (left.expr != null) {
Token start = ctx.start;
String fileName = currentFile;
int line = start.getLine();
int col = start.getCharPositionInLine();
if (left.expr instanceof IntIdentifier) {
if (op.equals("-")) {
((IntIdentifier) left.expr).multiplyByMinusOne();
}
me.expr = left.expr;
} else if (left.expr instanceof DoubleIdentifier) {
if (op.equals("-")) {
((DoubleIdentifier) left.expr).multiplyByMinusOne();
}
me.expr = left.expr;
} else {
Expression right = new IntIdentifier(1, fileName, line, col, line, col);
if (op.equals("-")) {
right = new IntIdentifier(-1, fileName, line, col, line, col);
}
Expression.BinaryOp bop = Expression.getBinaryOp("*");
BinaryExpression be = new BinaryExpression(bop);
be.setLeft(left.expr);
be.setRight(right);
me.expr = be;
}
setFileLineColumn(me.expr, ctx);
}
}
use of org.antlr.v4.runtime.Token in project compiler by boalang.
the class BaseTest method lex.
protected CommonTokenStream lex(final String input, final int[] ids, final String[] strings, final String[] errors) throws IOException {
final List<String> foundErr = new ArrayList<String>();
final BoaLexer lexer = new BoaLexer(new ANTLRInputStream(new StringReader(input)));
lexer.removeErrorListeners();
lexer.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
foundErr.add(line + "," + charPositionInLine + ": " + msg);
}
});
final CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
if (ids.length > 0 && strings.length > 0)
assertEquals("ids != strings", ids.length, strings.length);
if (ids.length > 0) {
final List<Token> t = tokens.getTokens();
if (DEBUG) {
for (int i = 0; i < t.size(); i++) {
final Token token = t.get(i);
System.out.print(token.getType() + ", ");
}
System.out.println();
for (int i = 0; i < t.size(); i++) {
final Token token = t.get(i);
System.out.print(token.getText() + ", ");
}
System.out.println();
System.out.println();
}
assertEquals("wrong number of tokens", ids.length, t.size());
for (int i = 0; i < t.size(); i++) assertEquals("wrong token type", ids[i], t.get(i).getType());
}
if (strings.length > 0) {
final List<Token> t = tokens.getTokens();
assertEquals("wrong number of tokens", strings.length, t.size());
for (int i = 0; i < t.size(); i++) assertEquals("wrong token type", strings[i], t.get(i).getText());
}
assertEquals("wrong number of errors: " + input, errors.length, foundErr.size());
for (int i = 0; i < foundErr.size(); i++) assertEquals("wrong error", errors[i], foundErr.get(i));
return tokens;
}
use of org.antlr.v4.runtime.Token in project CFLint by cflint.
the class CFLint method suppressed.
/*
* Look for a suppress comment on the same line. cflint:line - suppresses
* any messages on the same line cflint:MESSAGE_CODE - suppresses any
* message matching that code
*/
protected boolean suppressed(final BugInfo bugInfo, final Token token, final Context context) {
if (context == null || context.isSuppressed(bugInfo)) {
return true;
}
if (token == null) {
return false;
}
final Iterable<Token> tokens = context.afterTokens(token);
for (final Token currentTok : tokens) {
if (currentTok.getLine() != token.getLine()) {
break;
}
if (currentTok.getChannel() == Token.HIDDEN_CHANNEL && currentTok.getType() == CFSCRIPTLexer.LINE_COMMENT) {
final String commentText = currentTok.getText().replaceFirst("^//\\s*", "").trim();
if (commentText.startsWith("cflint ")) {
final Pattern pattern = Pattern.compile("cflint\\s+ignore:([\\w,]+).*");
final Matcher matcher = pattern.matcher(commentText);
if (matcher.matches() && matcher.groupCount() > 0) {
final String ignoreCodes = matcher.group(1);
if ("line".equalsIgnoreCase(ignoreCodes)) {
return true;
}
for (final String ignoreCode : ignoreCodes.split(",\\s*")) {
if (ignoreCode.equals(bugInfo.getMessageCode())) {
return true;
}
}
}
}
}
}
return false;
}
use of org.antlr.v4.runtime.Token in project CFLint by cflint.
the class CFLint method registerRuleOverrides.
/**
* @param context
* The current context.
* @param expression
* The expression statement to check
*/
protected void registerRuleOverrides(final Context context, final CFExpressionStatement expression) {
if (expression.getTokens() == null) {
return;
}
final Iterable<Token> tokens = expression.getTokens().getTokens();
for (final Token currentTok : tokens) {
if (currentTok.getLine() == expression.getExpression().getLine()) {
if (currentTok.getChannel() == Token.HIDDEN_CHANNEL && currentTok.getType() == CFSCRIPTLexer.LINE_COMMENT) {
final String commentText = currentTok.getText().replaceFirst("^//\\s*", "").trim();
if (commentText.startsWith("cflint ")) {
final Pattern pattern = Pattern.compile("cflint\\s+ignore:([\\w,]+).*");
final Matcher matcher = pattern.matcher(commentText);
if (matcher.matches()) {
final String ignoreCodes = matcher.group(1);
context.ignore(Arrays.asList(ignoreCodes.split(",\\s*")));
}
}
}
}
}
}
use of org.antlr.v4.runtime.Token in project hive by apache.
the class Stmt method createTable.
/**
* CREATE TABLE statement
*/
public Integer createTable(HplsqlParser.Create_table_stmtContext ctx) {
trace(ctx, "CREATE TABLE");
StringBuilder sql = new StringBuilder();
exec.append(sql, ctx.T_CREATE(), ctx.T_TABLE());
exec.append(sql, evalPop(ctx.table_name()).toString(), ctx.T_TABLE().getSymbol(), ctx.table_name().getStart());
Token last = ctx.table_name().getStop();
if (ctx.create_table_preoptions() != null) {
String preopt = evalPop(ctx.create_table_preoptions()).toString();
if (preopt != null) {
sql.append(" " + preopt);
}
last = ctx.create_table_preoptions().stop;
}
sql.append(createTableDefinition(ctx.create_table_definition(), last));
trace(ctx, sql.toString());
Query query = exec.executeSql(ctx, sql.toString(), exec.conf.defaultConnection);
if (query.error()) {
exec.signal(query);
return 1;
}
exec.setSqlSuccess();
exec.closeQuery(query, exec.conf.defaultConnection);
return 0;
}
Aggregations