Java Code Examples for org.antlr.v4.runtime.CharStreams#fromString()

The following examples show how to use org.antlr.v4.runtime.CharStreams#fromString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Queries.java    From elepy with Apache License 2.0 6 votes vote down vote up
public static Query parse(String input) {

        if (StringUtils.isEmpty(input)) {
            return Queries.empty();
        }
        //Lex (with Antlr's generated lexer)
        CharStream inputStream = CharStreams.fromString(input);
        EleQueryLexer lexer = new EleQueryLexer(inputStream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        //Parse (with Antlr's generated parser)
        EleQueryParser parser = new EleQueryParser(tokens);
        ParseTree parseTree = parser.query();

        //Extract AST from the Antlr parse tree
        QueryListener listener = new QueryListener();
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(listener, parseTree);

        return listener.getQuery();
    }
 
Example 2
Source File: StatementParserImpl.java    From jaybird with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public JaybirdStatementModel parseStatement(String sql) throws ParseException {
    try {
        CharStream stream = CharStreams.fromString(sql);
        JaybirdSqlLexer lexer = new JaybirdSqlLexer(stream);
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        
        JaybirdSqlParser parser = new JaybirdSqlParser(tokenStream);
        parser.removeErrorListeners();
        parser.statement();

        JaybirdStatementModel statementModel = parser.getStatementModel();
        if (statementModel.getStatementType() == JaybirdStatementModel.UNDETECTED_TYPE) {
            throw new ParseException("Unable to detect statement type or unsupported statement type");
        }
        if (statementModel.getTableName() == null) {
            throw new ParseException("Unable to parse query: no table name found");
        }
        return statementModel;
    } catch (RecognitionException e) {
        throw new ParseException("Unable to parse query", e);
    }
}
 
Example 3
Source File: EnumParseListenerTest.java    From protostuff-compiler with Apache License 2.0 6 votes vote down vote up
private Enum parseEnumBlock(String input) {
    CharStream stream = CharStreams.fromString(input);
    ProtoLexer lexer = new ProtoLexer(stream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(TestUtils.ERROR_LISTENER);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    ProtoParser parser = new ProtoParser(tokenStream);
    parser.setErrorHandler(new BailErrorStrategy());
    parser.removeErrorListeners();
    parser.addErrorListener(TestUtils.ERROR_LISTENER);
    ProtoContext context = new ProtoContext("test.proto");
    Proto proto = new Proto();
    context.push(proto);
    EnumParseListener enumParseListener = new EnumParseListener(tokenStream, context);
    OptionParseListener optionParseListener = new OptionParseListener(tokenStream, context);
    parser.addParseListener(enumParseListener);
    parser.addParseListener(optionParseListener);
    parser.enumBlock();
    return proto.getEnums().get(0);
}
 
Example 4
Source File: UserMatch.java    From onedev with MIT License 5 votes vote down vote up
public static UserMatch parse(@Nullable String userMatchString) {
	List<UserMatchCriteria> criterias = new ArrayList<>();
	List<UserMatchCriteria> exceptCriterias = new ArrayList<>();
	
	if (userMatchString != null) {
		CharStream is = CharStreams.fromString(userMatchString); 
		UserMatchLexer lexer = new UserMatchLexer(is);
		lexer.removeErrorListeners();
		lexer.addErrorListener(new BaseErrorListener() {

			@Override
			public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
					int charPositionInLine, String msg, RecognitionException e) {
				throw new RuntimeException("Malformed user match");
			}
			
		});
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		UserMatchParser parser = new UserMatchParser(tokens);
		parser.removeErrorListeners();
		parser.setErrorHandler(new BailErrorStrategy());
		
		UserMatchContext userMatchContext = parser.userMatch();
		
		for (CriteriaContext criteriaContext: userMatchContext.criteria())
			criterias.add(getUserMatchCriteria(criteriaContext));
		
		for (ExceptCriteriaContext exceptCriteriaContext: userMatchContext.exceptCriteria()) {
			exceptCriterias.add(getUserMatchCriteria(exceptCriteriaContext.criteria()));
		}
	}
	
	return new UserMatch(criterias, exceptCriterias);
}
 
Example 5
Source File: GrammarParameterizedTest.java    From jaybird with GNU Lesser General Public License v2.1 5 votes vote down vote up
private JaybirdSqlParser createParser(String testString) {
    CharStream stream = CharStreams.fromString(testString);

    JaybirdSqlLexer lexer = new JaybirdSqlLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);

    return new JaybirdSqlParser(tokenStream);
}
 
Example 6
Source File: Parser.java    From cava with Apache License 2.0 5 votes vote down vote up
static List<String> parseDottedKey(String dottedKey) {
  TomlLexer lexer = new TomlLexer(CharStreams.fromString(dottedKey));
  lexer.mode(TomlLexer.KeyMode);
  TomlParser parser = new TomlParser(new CommonTokenStream(lexer));
  parser.removeErrorListeners();
  AccumulatingErrorListener errorListener = new AccumulatingErrorListener();
  parser.addErrorListener(errorListener);
  List<String> keyList = parser.tomlKey().accept(new KeyVisitor());
  List<TomlParseError> errors = errorListener.errors();
  if (!errors.isEmpty()) {
    TomlParseError e = errors.get(0);
    throw new IllegalArgumentException("Invalid key: " + e.getMessage(), e);
  }
  return keyList;
}
 
Example 7
Source File: ZserioLexerTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void notTerminatedMultilineStringLiteral()
{
    final CharStream input = CharStreams.fromString(
            "\"Not terminated\nmultiline string\""
    );

    ZserioLexer lexer = new ZserioLexer(input);
    checkToken(lexer, ZserioLexer.INVALID_STRING_LITERAL, "\"Not terminated\nmultiline string\"");
    checkEOF(lexer);
}
 
Example 8
Source File: SyntaxTest.java    From protostuff-compiler with Apache License 2.0 5 votes vote down vote up
private void parseSyntaxLine(String input) {
    CharStream stream = CharStreams.fromString(input);
    ProtoLexer lexer = new ProtoLexer(stream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(TestUtils.ERROR_LISTENER);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    ProtoParser parser = new ProtoParser(tokenStream);
    parser.addErrorListener(TestUtils.ERROR_LISTENER);
    parser.syntaxStatement();
}
 
Example 9
Source File: CompositeFileReaderTest.java    From protostuff-compiler with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public CharStream read(String name) {
    if (this.name.equals(name)) {
        return CharStreams.fromString(text);
    }
    return null;
}
 
Example 10
Source File: Python3MethodParser.java    From Siamese with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parses an input file and return the root RuleContext.
 * @param filePath Location of input file
 * @return root RuleContext
 */
private static Python3Parser.File_inputContext parseFile(String filePath) {
    File file = new File(filePath);
    String sourceCode = readFile(file);

    Python3Lexer lexer = new Python3Lexer(CharStreams.fromString(sourceCode));
    TokenStream tokenStream = new CommonTokenStream(lexer);
    Python3Parser parser = new Python3Parser(tokenStream);

    return parser.file_input();
}
 
Example 11
Source File: FHIRPathUtil.java    From FHIR with Apache License 2.0 5 votes vote down vote up
public static ExpressionContext compile(String expr) {
    FHIRPathLexer lexer = new FHIRPathLexer(CharStreams.fromString(expr));
    lexer.removeErrorListeners();
    lexer.addErrorListener(SYNTAX_ERROR_LISTENER);

    CommonTokenStream tokens = new CommonTokenStream(lexer);

    FHIRPathParser parser = new FHIRPathParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SYNTAX_ERROR_LISTENER);

    return parser.expression();
}
 
Example 12
Source File: TypeCalculation.java    From presto with Apache License 2.0 5 votes vote down vote up
private static ParserRuleContext parseTypeCalculation(String calculation)
{
    TypeCalculationLexer lexer = new TypeCalculationLexer(new CaseInsensitiveStream(CharStreams.fromString(calculation)));
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    TypeCalculationParser parser = new TypeCalculationParser(tokenStream);

    lexer.removeErrorListeners();
    lexer.addErrorListener(ERROR_LISTENER);

    parser.removeErrorListeners();
    parser.addErrorListener(ERROR_LISTENER);

    ParserRuleContext tree;
    try {
        // first, try parsing with potentially faster SLL mode
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
        tree = parser.typeCalculation();
    }
    catch (ParseCancellationException ex) {
        // if we fail, parse with LL mode
        tokenStream.seek(0); // rewind input stream
        parser.reset();

        parser.getInterpreter().setPredictionMode(PredictionMode.LL);
        tree = parser.typeCalculation();
    }
    return tree;
}
 
Example 13
Source File: StackTracesParsing.java    From botsing with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the given input and returns a list of stack traces found in that input.
 * @param input The input to parse.
 * @return A list with the stack traces parsed from the given input.
 */
public static List<CrashStackTrace> parseStackTraces(String input){
    CharStream text = CharStreams.fromString(input);
    StackTracesLexer lexer = new StackTracesLexer(text);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    StackTracesParser parser = new StackTracesParser(tokens);
    StackTracesParser.StackTracesContext tree = parser.stackTraces();
    // Walk the three to build the stack traces
    StackTracesCollectorListener listener = new StackTracesCollectorListener();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(listener, tree);
    // Return the built list
    return listener.getStackTraces();
}
 
Example 14
Source File: SiddhiCompiler.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public static Partition parsePartition(String partition) throws SiddhiParserException {
    CharStream input = CharStreams.fromString(partition);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);

    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.partition_final();

    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (Partition) eval.visit(tree);
}
 
Example 15
Source File: PatternSet.java    From onedev with MIT License 5 votes vote down vote up
public static PatternSet parse(@Nullable String patternSetString) {
	Set<String> includes = new HashSet<>();
	Set<String> excludes = new HashSet<>();
	
	if (patternSetString != null) {
		CharStream is = CharStreams.fromString(patternSetString); 
		PatternSetLexer lexer = new PatternSetLexer(is);
		lexer.removeErrorListeners();
		lexer.addErrorListener(new BaseErrorListener() {

			@Override
			public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
					int charPositionInLine, String msg, RecognitionException e) {
				throw new RuntimeException("Malformed pattern set");
			}
			
		});
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		PatternSetParser parser = new PatternSetParser(tokens);
		parser.removeErrorListeners();
		parser.setErrorHandler(new BailErrorStrategy());
		
		PatternsContext patternsContext = parser.patterns();
		
		for (PatternContext pattern: patternsContext.pattern()) {
			String value;
			if (pattern.Quoted() != null) 
				value = FenceAware.unfence(pattern.Quoted().getText());
			else 
				value = pattern.NQuoted().getText();
			value = StringUtils.unescape(value);
			if (pattern.Excluded() != null)
				excludes.add(value);
			else 
				includes.add(value);
		}			
	}
	
	return new PatternSet(includes, excludes);
}
 
Example 16
Source File: SqlParser.java    From crate with Apache License 2.0 5 votes vote down vote up
private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction) {
    try {
        SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(CharStreams.fromString(sql, name)));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        SqlBaseParser parser = new SqlBaseParser(tokenStream);

        parser.addParseListener(new PostProcessor());

        lexer.removeErrorListeners();
        lexer.addErrorListener(ERROR_LISTENER);

        parser.removeErrorListeners();
        parser.addErrorListener(ERROR_LISTENER);

        ParserRuleContext tree;
        try {
            // first, try parsing with potentially faster SLL mode
            parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            tree = parseFunction.apply(parser);
        } catch (ParseCancellationException ex) {
            // if we fail, parse with LL mode
            tokenStream.seek(0); // rewind input stream
            parser.reset();

            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            tree = parseFunction.apply(parser);
        }

        return new AstBuilder().visit(tree);
    } catch (StackOverflowError e) {
        throw new ParsingException(name + " is too large (stack overflow while parsing)");
    }
}
 
Example 17
Source File: MatcherAction.java    From yauaa with Apache License 2.0 4 votes vote down vote up
public long initialize() {
    InitErrorListener errorListener = new InitErrorListener();

    CodePointCharStream input = CharStreams.fromString(this.matchExpression);
    UserAgentTreeWalkerLexer lexer = new UserAgentTreeWalkerLexer(input);

    lexer.addErrorListener(errorListener);

    CommonTokenStream tokens = new CommonTokenStream(lexer);
    UserAgentTreeWalkerParser parser = new UserAgentTreeWalkerParser(tokens);

    parser.addErrorListener(errorListener);

    ParserRuleContext requiredPattern = parseWalkerExpression(parser);

    // We couldn't ditch the double quotes around the fixed values in the parsing phase.
    // So we ditch them here. We simply walk the tree and modify some of the tokens.
    new UnQuoteValues().visit(requiredPattern);

    // Now we create an evaluator instance
    evaluator = new TreeExpressionEvaluator(requiredPattern, matcher, verbose);

    // Is a fixed value (i.e. no events will ever be fired)?
    String fixedValue = evaluator.getFixedValue();
    if (fixedValue != null) {
        setFixedValue(fixedValue);
        mustHaveMatches = false;
        matches = new MatchesList(0);
        return 0; // Not interested in any patterns
    }

    mustHaveMatches = evaluator.mustHaveMatches();

    int informs = calculateInformPath(this, "agent", requiredPattern);

    // If this is based on a variable we do not need any matches from the hashmap.
    if (mustHaveMatches && informs == 0) {
        mustHaveMatches = false;
    }

    int listSize = 0;
    if (informs > 0) {
        listSize = 1;
    }
    this.matches = new MatchesList(listSize);
    return informs;
}
 
Example 18
Source File: REPLShell.java    From Concurnas with MIT License 4 votes vote down vote up
@Override
public AttributedString highlight(LineReader reader, String buffer) {
	
	LexParseErrorCapturer errs = new LexParseErrorCapturer("");
	
	CharStream input = CharStreams.fromString(buffer, "");
	ConcurnasLexer lexer = new ConcurnasLexer(input);
	lexer.removeErrorListeners();
	lexer.addErrorListener(errs);
	
	List<? extends Token> toks = lexer.getAllTokens();
	if(toks.isEmpty() || !errs.errors.isEmpty()) {
		return AttributedString.fromAnsi(buffer);//do nothing
	}
	
	int cursor = 0;
	
	StringBuilder sb = new StringBuilder();
	for(Token tok : toks) {
		int startIdx = tok.getStartIndex();
		while(cursor++ < startIdx-1) {
			sb.append(" ");//add whitespace padding
		}
		
		String tokenName = ConcurnasLexer.VOCABULARY.getDisplayName(tok.getType());
		
		int color = Color_Default;
		if(null != tokenName && tokenToColor.containsKey(tokenName)) {
			color = tokenToColor.get(tokenName);
		}

		boolean escaped = buffer.charAt(startIdx) == '\\';
		
		sb.append(String.format("\u001b[38;5;%sm%s", color, (escaped?"\\" : "") + tok.getText()));
		cursor = tok.getStopIndex();
	}
	
	int len = buffer.length();
	while(cursor++ < len-1) {
		sb.append(" ");//trailing spaces
	}
	return AttributedString.fromAnsi(sb.toString());
}
 
Example 19
Source File: NormalizeDateTimeLiteralsTransformer.java    From jdmn with Apache License 2.0 4 votes vote down vote up
private Lexer makeLexer(String text) {
    CharStream cs = CharStreams.fromString(text);
    return new FEELLexer(cs);
}
 
Example 20
Source File: AntlrSQLitelParserTest.java    From Knowage-Server with GNU Affero General Public License v3.0 3 votes vote down vote up
public static void main( String[] args )
	{
		String query = "select SUM(prova) pippo pluto";

	//	String query = "pippo";
		CharStream inputStream = CharStreams.fromString(query);

		SQLiteLexer tokenSource = new SQLiteLexer(new CaseChangingCharStream(inputStream, true));


 		TokenStream tokenStream = new CommonTokenStream(tokenSource);
 		SQLiteParser sQLiteParser = new SQLiteParser(tokenStream);

 		sQLiteParser.addErrorListener(ThrowingErrorListener.INSTANCE);
		ParseTree root = sQLiteParser.select_stmt();


		try {

		System.out.println( 	root.toStringTree());
		System.out.println( sQLiteParser.getNumberOfSyntaxErrors());

		}
		catch(Exception e) {
			System.out.println(e);
		}


// 		MySqlParserBaseVisitor visitor = new MySqlParserVisitorImpl();
//		visitor.visit(root);
	}