-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from TreshMom/task-11
Перелив для 12 таски
- Loading branch information
Showing
10 changed files
with
91 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,7 @@ celerybeat.pid | |
# Environments | ||
.env | ||
.venv | ||
project/lang | ||
env/ | ||
venv/ | ||
ENV/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
grammar lang; | ||
|
||
WS: [ \r\n\t]+ -> skip; | ||
|
||
prog: stmt* EOF; | ||
|
||
stmt: bind | add | remove | declare; | ||
|
||
declare: 'let' VAR 'is' 'graph'; | ||
bind: 'let' VAR '=' expr; | ||
remove: 'remove' ('vertex' | 'edge' | 'vertices') expr 'from' VAR; | ||
add: 'add' ('vertex'|'edge') expr 'to' VAR; | ||
|
||
|
||
expr: NUM | CHAR | VAR | edge_expr | set_expr | regexp | select; | ||
|
||
set_expr: '[' expr (',' expr)* ']'; | ||
edge_expr: '(' expr ',' expr ',' expr ')'; | ||
regexp: | ||
CHAR | ||
| VAR | ||
| '(' regexp ')' | ||
| regexp '|' regexp | ||
| regexp '^' range | ||
| regexp '.' regexp | ||
| regexp '&' regexp; | ||
|
||
range: '[' NUM '..' NUM? ']'; | ||
select: v_filter? v_filter? 'return' VAR (',' VAR)? 'where' VAR 'reachable' | ||
'from' VAR 'in' VAR 'by' expr; | ||
|
||
v_filter: 'for' VAR 'in' expr; | ||
|
||
VAR: [a-z] [a-z0-9]*; | ||
NUM: '0' | ([1-9] [0-9]*); | ||
CHAR: '"' [a-z] '"'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from project.lang.langLexer import langLexer | ||
from project.lang.langParser import langParser | ||
from project.lang.langListener import langListener | ||
|
||
from antlr4 import ParserRuleContext, CommonTokenStream | ||
from antlr4.InputStream import InputStream | ||
|
||
|
||
class NodeCountListener(langListener): | ||
|
||
def __init__(self) -> None: | ||
super(langListener, self).__init__() | ||
self.count = 0 | ||
|
||
def enterEveryRule(self, ctx): | ||
self.count += 1 | ||
|
||
def get_count(self): | ||
return self.count | ||
|
||
|
||
class StringifyListener(langListener): | ||
|
||
def __init__(self): | ||
super(langListener, self).__init__() | ||
self.result = "" | ||
|
||
def enterEveryRule(self, rule): | ||
self.result += rule.getText() | ||
|
||
def get_result(self): | ||
return self.result | ||
|
||
|
||
def prog_to_tree(program: str) -> tuple[ParserRuleContext, bool]: | ||
parser = langParser(CommonTokenStream(langLexer(InputStream(program)))) | ||
return parser.prog(), (parser.getNumberOfSyntaxErrors() == 0) | ||
|
||
|
||
def nodes_count(tree: ParserRuleContext) -> int: | ||
listener = NodeCountListener() | ||
tree.enterRule(listener) | ||
return listener.get_count() | ||
|
||
|
||
def tree_to_prog(tree: ParserRuleContext) -> str: | ||
listener = StringifyListener() | ||
tree.enterRule(listener) | ||
return listener.get_result() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.