Added Parser

This commit is contained in:
Sven Heidemann 2021-10-27 09:18:57 +02:00
parent 1f6d13551b
commit 4af133ab21
9 changed files with 56 additions and 6 deletions

View File

@ -6,6 +6,7 @@ from cpl_core.console import Console
from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.dependency_injection import ServiceProviderABC
from lexer.abc.lexer_abc import LexerABC from lexer.abc.lexer_abc import LexerABC
from parser.abc.parser_abc import ParserABC
from runtime.abc.runtime_service_abc import RuntimeServiceABC from runtime.abc.runtime_service_abc import RuntimeServiceABC
@ -15,19 +16,20 @@ class Application(ApplicationABC):
ApplicationABC.__init__(self, config, services) ApplicationABC.__init__(self, config, services)
self._lexer: LexerABC = services.get_service(LexerABC) self._lexer: LexerABC = services.get_service(LexerABC)
self._parser: ParserABC = services.get_service(ParserABC)
self._runtime: RuntimeServiceABC = services.get_service(RuntimeServiceABC) self._runtime: RuntimeServiceABC = services.get_service(RuntimeServiceABC)
self._path = config.get_configuration('p') self._path = config.get_configuration('p')
Console.write_line('t', self._path)
def _interpret(self, line: str): def _interpret(self, line: str):
tokens = self._lexer.tokenize(line) tokens = self._lexer.tokenize(line)
ast = self._parser.create_ast(tokens)
line.replace("\n", "").replace("\t", "") line.replace("\n", "").replace("\t", "")
Console.write_line(f'<{self._runtime.line_count}> LINE: {line}') # Console.write_line(f'<{self._runtime.line_count}> LINE: {line}')
header, values = ['Type', 'Value'], [] # header, values = ['Type', 'Value'], []
tokens.for_each(lambda t: values.append([t.type, t.value])) # tokens.for_each(lambda t: values.append([t.type, t.value]))
Console.table(header, values) # Console.table(header, values)
def _console(self): def _console(self):
i = 0 i = 0

View File

@ -5,6 +5,8 @@ from cpl_core.environment import ApplicationEnvironment
from lexer.abc.lexer_abc import LexerABC from lexer.abc.lexer_abc import LexerABC
from lexer.service.lexer_service import LexerService from lexer.service.lexer_service import LexerService
from parser.abc.parser_abc import ParserABC
from parser.service.parser_service import ParserService
from runtime.abc.runtime_service_abc import RuntimeServiceABC from runtime.abc.runtime_service_abc import RuntimeServiceABC
from runtime.service.runtime_service import RuntimeService from runtime.service.runtime_service import RuntimeService
@ -22,6 +24,7 @@ class Startup(StartupABC):
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironment) -> ServiceProviderABC: def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironment) -> ServiceProviderABC:
services.add_singleton(LexerABC, LexerService) services.add_singleton(LexerABC, LexerService)
services.add_singleton(ParserABC, ParserService)
services.add_singleton(RuntimeServiceABC, RuntimeService) services.add_singleton(RuntimeServiceABC, RuntimeService)
return services.build_service_provider() return services.build_service_provider()

View File

@ -18,3 +18,6 @@ class Token:
@value.setter @value.setter
def value(self, value: str): def value(self, value: str):
self._value = value self._value = value
def __str__(self) -> str:
return f'Token: Type: {self._type}, Value: {self._value}'

View File

@ -1 +1 @@
# imports: # imports

View File

@ -0,0 +1 @@
# imports

View File

@ -0,0 +1,7 @@
from abc import ABC, abstractmethod
class AST(ABC):
@abstractmethod
def __init__(self): pass

View File

@ -0,0 +1,14 @@
from abc import ABC, abstractmethod
from cpl_query.extension.list import List
from parser.abc.ast_abc import AST
from lexer.model.token import Token
class ParserABC(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
def create_ast(self, tokens: List[Token]) -> List[AST]: pass

View File

@ -0,0 +1 @@
# imports

View File

@ -0,0 +1,19 @@
from cpl_core.console.console import Console
from cpl_query.extension.list import List
from lexer.model.token import Token
from parser.abc.ast_abc import AST
from parser.abc.parser_abc import ParserABC
class ParserService(ParserABC):
def __init__(self):
pass
def create_ast(self, tokens: List[Token]) -> List[AST]:
for i in range(0, tokens.count()):
prev_token = tokens[i-1] if i-1 > 0 else None
token = tokens[i]
next_token = tokens[i+1] if i+1 < tokens.count() else None
Console.write_line(token)