LightJason - AgentSpeak(L++)
IBaseParser.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.grammar;
25 
26 import org.antlr.v4.runtime.ANTLRErrorListener;
27 import org.antlr.v4.runtime.CharStream;
28 import org.antlr.v4.runtime.CharStreams;
29 import org.antlr.v4.runtime.CommonTokenStream;
30 import org.antlr.v4.runtime.Lexer;
31 import org.antlr.v4.runtime.Parser;
32 import org.antlr.v4.runtime.TokenStream;
33 
34 import javax.annotation.Nonnull;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.lang.reflect.Constructor;
38 import java.lang.reflect.InvocationTargetException;
39 
40 
44 public abstract class IBaseParser<T extends IASTVisitor, L extends Lexer, P extends Parser> implements IParser<T>
45 {
49  private final ANTLRErrorListener m_errorlistener;
53  private final Constructor<L> m_ctorlexer;
57  private final Constructor<P> m_ctorparser;
58 
59 
66  protected IBaseParser( @Nonnull final ANTLRErrorListener p_errorlistener ) throws NoSuchMethodException
67  {
68  m_errorlistener = p_errorlistener;
69  m_ctorlexer = this.lexerclass().getConstructor( CharStream.class );
70  m_ctorparser = this.parserclass().getConstructor( TokenStream.class );
71  }
72 
84  protected final P parser( @Nonnull final InputStream p_stream ) throws IOException, IllegalAccessException, InvocationTargetException, InstantiationException
85  {
86  final L l_lexer = m_ctorlexer.newInstance( CharStreams.fromStream( p_stream ) );
87  l_lexer.removeErrorListeners();
88  l_lexer.addErrorListener( m_errorlistener );
89 
90  final P l_parser = m_ctorparser.newInstance( new CommonTokenStream( l_lexer ) );
91  l_parser.removeErrorListeners();
92  l_parser.addErrorListener( m_errorlistener );
93 
94  return l_parser;
95  }
96 
102  protected abstract Class<L> lexerclass();
103 
109  protected abstract Class<P> parserclass();
110 }
abstract Class< P > parserclass()
returns the parser class reference
final Constructor< L > m_ctorlexer
ctor lexer reference
final Constructor< P > m_ctorparser
ctor parser reference
IBaseParser( @Nonnull final ANTLRErrorListener p_errorlistener)
ctor
final P parser( @Nonnull final InputStream p_stream)
returns a parser component
final ANTLRErrorListener m_errorlistener
error listener
abstract Class< L > lexerclass()
returns the lexer class reference