org.antlr.runtime.ANTLRReaderStream Java Examples

The following examples show how to use org.antlr.runtime.ANTLRReaderStream. 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: DRLFactory.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public static DRLLexer buildLexer( Reader reader, LanguageLevelOption languageLevel ) {
    try {
        return getDRLLexer(new ANTLRReaderStream(reader), languageLevel);
    } catch ( final Exception e ) {
        throw new RuntimeException( "Unable to parser Reader", e );
    }
}
 
Example #2
Source File: DRLFactory.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public static DRLParser buildParser( Reader reader, LanguageLevelOption languageLevel ) {
    try {
        return buildParser(new ANTLRReaderStream(reader), languageLevel);
    } catch ( final Exception e ) {
        throw new RuntimeException( "Unable to parser Reader", e );
    }
}
 
Example #3
Source File: HTMLParser.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * The only method that should be called to initiate the process
 * 
 * @param is
 *            The input stream from where to get the data
 * @param os
 *            The output stream to write the processed fragment/document to
 * @param htmlFilter
 *            An interface called during the processing of the document. Can
 *            be used to modify elements
 * @param convertIntoValidXML
 *            Converts the output into valid XML for XSL processing for
 *            example
 */
public static void process(Reader reader, Writer writer, IHTMLFilter htmlFilter, boolean convertIntoValidXML)
		throws HandlingException {
	try {
		// Open a char stream input for the document
		ANTLRStringStream input = new ANTLRReaderStream(reader);

		// Start lexing the input
		htmlLexerLexer lex = new htmlLexerLexer(input);

		// Tokenstream for the parser.
		CommonTokenStream tokens = new CommonTokenStream(lex);
		htmlParserParser parser = new htmlParserParser(tokens);
		htmlParserParser.document_return root = parser.document();

		// Set up the tree parser
		CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree) root.getTree());
		htmlTreeParser walker = new htmlTreeParser(nodes);

		// Initialize data structures
		topNode = new ThreadLocal<>();
		currentNode = new ThreadLocal<>();
		attrNode = new ThreadLocal<>();

		// Walk in the entire document using the tree parser.
		walker.document();

		// Get the top node
		TagNode top = (TagNode) topNode.get();

		// Write the clean document out.
		top.writeAll(writer, htmlFilter, convertIntoValidXML, false);
	} catch (IOException ioe) {
		throw new HandlingException("Could not parse document");
	} catch (RecognitionException re) {
		throw new HandlingException("Could not parse document");
	}
}
 
Example #4
Source File: AbstractAntlrParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IParseResult doParse(Reader reader) {
	try {
		return parse(getDefaultRuleName(), new ANTLRReaderStream(reader));
	} catch (IOException e) {
		throw new WrappedException(e);
	}
}
 
Example #5
Source File: AbstractAntlrParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IParseResult parse(ParserRule rule, Reader reader) {
	try {
		IParseResult parseResult = parse(rule.getName(), new ANTLRReaderStream(reader));
		return parseResult;
	} catch (IOException e) {
		throw new WrappedException(e);
	}
}
 
Example #6
Source File: AbstractAntlrParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IParseResult parse(RuleCall ruleCall, Reader reader, int initialLookAhead) {
	try {
		NodeModelBuilder builder = nodeModelBuilder.get();
		builder.setForcedFirstGrammarElement(ruleCall);
		IParseResult parseResult = doParse(ruleCall.getRule().getName(), new ANTLRReaderStream(reader), builder, initialLookAhead);
		return parseResult;
	} catch (IOException e) {
		throw new WrappedException(e);
	}
}
 
Example #7
Source File: AbstractTokenSourceProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected CharStream getCharStream(Reader reader) {
	try {
		return new ANTLRReaderStream(reader);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example #8
Source File: AbstractContextualAntlrParser.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}.
 * For parser delegation it is critically important that the root node of the parsed sub-tree is not merged. Otherwise we do not have a proper new composite
 * node for replacement.
 */
public IParseResult parseDelegate(final ParserRule rule, final Reader reader) {
  try {
    NodeModelBuilder builder = createNodeModelBuilder();
    builder.setForcedFirstGrammarElement(null);
    return doParse(rule.getName(), new ANTLRReaderStream(reader), builder, 0);
  } catch (IOException e) {
    throw new WrappedException(e);
  }
}
 
Example #9
Source File: ProtoUtil.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the proto from an {@link ANTLRReaderStream}.
 */
public static void loadFrom(ANTLRReaderStream input, Proto target) throws Exception
{
    // Create an ExprLexer that feeds from that stream
    ProtoLexer lexer = new ProtoLexer(input);
    // Create a stream of tokens fed by the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Create a parser that feeds off the token stream
    ProtoParser parser = new ProtoParser(tokens);
    // Begin parsing at rule parse
    parser.parse(target);
}
 
Example #10
Source File: DSLTokenizedMappingFile.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
private DSLMapping buildFileMapping(final List<ParserError> errors, final Reader dsl) throws IOException, RecognitionException{
    ANTLRReaderStream reader = new ANTLRReaderStream(dsl);
    DSLMapWalker walker = buildFileMappingWalker(errors, reader);
    DSLMapping mapping = walker.mapping_file();
    return mapping;
}
 
Example #11
Source File: Antlr4ToolFacade.java    From openCypher with Apache License 2.0 4 votes vote down vote up
public static void assertGeneratesValidParser( String resource ) throws Exception
{
    Output.Readable buffer = stringBuilder();
    Tool antlr = new Tool();
    Antlr4ToolFacade facade = new Antlr4ToolFacade( antlr, buffer );
    try
    {
        Antlr4.write( Fixture.grammarResource( Antlr4.class, resource ), buffer );
    }
    catch ( Throwable e )
    {
        try
        {
            facade.reportFailureIn( "generating grammar" );
        }
        catch ( AssertionError x )
        {
            throw e;
        }
    }
    antlr.addListener( facade );
    GrammarRootAST ast = antlr.parse( resource, new ANTLRReaderStream( buffer.reader() ) );
    if ( ast.hasErrors )
    {
        RuleAST lastGood = lastGoodRule( ast );
        if ( lastGood == null )
        {
            facade.reportFailureIn( "parsing grammar" );
        }
        else
        {
            facade.reportFailureIn(
                    "parsing grammar, after " + lastGood.getRuleName() + " on line " + lastGood.getLine() );
        }
    }
    antlr.process( antlr.createGrammar( ast ), false );
    if ( facade.hasErrors() )
    {
        facade.reportFailureIn( "processing grammar" );
    }
}
 
Example #12
Source File: ProtoUtil.java    From protostuff with Apache License 2.0 4 votes vote down vote up
/**
 * Loads the proto from a {@link Reader}.
 */
public static void loadFrom(Reader reader, Proto target) throws Exception
{
    loadFrom(new ANTLRReaderStream(reader), target);
}