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
+32
View File
@@ -8,6 +8,7 @@ class TokenKind(Enum):
NEWLINE = "newline"
KEYWORD = "keyword"
IDENTIFIER = "identifier"
CONCEPT = "concept"
STRING = "string"
NUMBER = "number"
TRUE = "true"
@@ -210,6 +211,11 @@ class Tokenizer:
self.i += len(newline)
self.column = 1
self.line += 1
elif c == "c" and self.i + 1 < self.text_len and self.text[self.i + 1] == ":":
concept_name = self.eat_concept_name(self.i + 2, self.line, self.column)
yield Token(TokenKind.CONCEPT, concept_name, self.i, self.line, self.column)
self.i += len(concept_name) + 3
self.column += len(concept_name) + 3
elif c.isalpha() or c == "_":
identifier = self.eat_identifier(self.i)
token_type = TokenKind.KEYWORD if identifier in self.KEYWORDS else TokenKind.IDENTIFIER
@@ -233,6 +239,32 @@ class Tokenizer:
yield Token(TokenKind.EOF, "", self.i, self.line, self.column)
def eat_concept_name(self, start, line, column):
result = ""
i = start
end_colon_found = False
while i < self.text_len:
c = self.text[i]
if c == "\n":
raise LexerError(f"New line is forbidden in concept name", result, i, line, column + 2 + len(result))
if c == ":":
end_colon_found = True
break
result += c
i += 1
if not end_colon_found:
raise LexerError(f"Missing ending colon", result, i, line, column + 2 + len(result))
if result == "":
raise LexerError(f"Context name not found", result, start, line, column + 2 + len(result))
return result
def eat_whitespace(self, start):
result = self.text[start]
i = start + 1