Added keyword c:xxx: to express that we want the concept, not its body

This commit is contained in:
2019-12-29 18:56:41 +01:00
parent 81b2355633
commit 197b0700fa
9 changed files with 191 additions and 65 deletions
+6 -1
View File
@@ -86,7 +86,12 @@ class BaseParser:
if not hasattr(tokens, "__iter__"):
tokens = [tokens]
switcher = {
TokenKind.KEYWORD: lambda t: Keywords(t.value).value,
TokenKind.CONCEPT: lambda t: f"__C__{t.value}__C__"
}
for token in tokens:
value = Keywords(token.value).value if token.type == TokenKind.KEYWORD else token.value
value = switcher.get(token.type, lambda t: t.value)(token)
res += value
return res
+12 -4
View File
@@ -1,4 +1,5 @@
from core.builtin_concepts import BuiltinConcepts
from core.tokenizer import Tokenizer
from parsers.BaseParser import BaseParser, Node, ErrorNode
from dataclasses import dataclass
import ast
@@ -62,16 +63,23 @@ class PythonParser(BaseParser):
self.source = kwargs.get("source", "<undef>")
def parse(self, context, text):
text = text if isinstance(text, str) else self.get_text_from_tokens(text)
text = text.strip()
if isinstance(text, str) and "c:" in text:
source = self.get_text_from_tokens(list(Tokenizer(text)))
elif isinstance(text, str):
source = text
else:
source = self.get_text_from_tokens(text)
source = source.strip()
text = text if isinstance(text, str) else source
sheerka = context.sheerka
# first, try to parse an expression
res, tree, error = self.try_parse_expression(text)
res, tree, error = self.try_parse_expression(source)
if not res:
# then try to parse a statement
res, tree, error = self.try_parse_statement(text)
res, tree, error = self.try_parse_statement(source)
if not res:
self.has_error = True
error_node = PythonErrorNode(text, error)