This commit is contained in:
Erez Shinan 2018-05-24 15:20:55 +03:00
parent 47ff0e99fc
commit 895e056e74
1 changed files with 20 additions and 44 deletions

View File

@ -7,8 +7,6 @@ from PyQt5.Qt import * # noqa
from PyQt5.Qsci import QsciScintilla from PyQt5.Qsci import QsciScintilla
from PyQt5.Qsci import QsciLexerCustom from PyQt5.Qsci import QsciLexerCustom
from editor import Editor
from lark import Lark, inline_args, Transformer from lark import Lark, inline_args, Transformer
@ -45,54 +43,30 @@ class LexerJson(QsciLexerCustom):
self.token_styles = { self.token_styles = {
"__COLON": 5, "__COLON": 5,
"__COMMA": 5, "__COMMA": 5,
"__FALSE1": 0,
"__LBRACE": 5, "__LBRACE": 5,
"__LSQB": 5, "__LSQB": 5,
"__NULL2": 0,
"__RBRACE": 5, "__RBRACE": 5,
"__RSQB": 5, "__RSQB": 5,
"__TRUE0": 0, "FALSE": 0,
"ESCAPED_STRING": 4, "NULL": 0,
"SIGNED_NUMBER": 1, "TRUE": 0,
"STRING": 4,
"NUMBER": 1,
} }
def create_grammar(self): def create_grammar(self):
grammar = ''' grammar = '''
?start: value anons: ":" "{" "}" "," "[" "]"
?value: object TRUE: "true"
| array FALSE: "false"
| string NULL: "NULL"
| SIGNED_NUMBER -> number %import common.ESCAPED_STRING -> STRING
| "true" -> true %import common.SIGNED_NUMBER -> NUMBER
| "false" -> false
| "null" -> null
array : "[" [value ("," value)*] "]"
object : "{" [pair ("," pair)*] "}"
pair : string ":" value
string : ESCAPED_STRING
%import common.ESCAPED_STRING
%import common.SIGNED_NUMBER
%import common.WS %import common.WS
%ignore WS %ignore WS
''' '''
class TreeToJson(Transformer): self.lark = Lark(grammar, parser=None, lexer='standard')
@inline_args
def string(self, s):
return s[1:-1].replace('\\"', '"')
array = list
pair = tuple
object = dict
number = inline_args(float)
def null(self, _): return None
def true(self, _): return True
def false(self, _): return False
self.lark = Lark(grammar, parser='lalr', transformer=TreeToJson())
# All tokens: print([t.name for t in self.lark.parser.lexer.tokens]) # All tokens: print([t.name for t in self.lark.parser.lexer.tokens])
def defaultPaper(self, style): def defaultPaper(self, style):
@ -173,11 +147,7 @@ class EditorAll(QsciScintilla):
self.setLexer(lexer) self.setLexer(lexer)
def main(): EXAMPLE_TEXT = textwrap.dedent("""\
app = QApplication(sys.argv)
ex = EditorAll()
ex.setWindowTitle(__file__)
ex.setText(textwrap.dedent("""\
{ {
"_id": "5b05ffcbcf8e597939b3f5ca", "_id": "5b05ffcbcf8e597939b3f5ca",
"about": "Excepteur consequat commodo esse voluptate aute aliquip ad sint deserunt commodo eiusmod irure. Sint aliquip sit magna duis eu est culpa aliqua excepteur ut tempor nulla. Aliqua ex pariatur id labore sit. Quis sit ex aliqua veniam exercitation laboris anim adipisicing. Lorem nisi reprehenderit ullamco labore qui sit ut aliqua tempor consequat pariatur proident.", "about": "Excepteur consequat commodo esse voluptate aute aliquip ad sint deserunt commodo eiusmod irure. Sint aliquip sit magna duis eu est culpa aliqua excepteur ut tempor nulla. Aliqua ex pariatur id labore sit. Quis sit ex aliqua veniam exercitation laboris anim adipisicing. Lorem nisi reprehenderit ullamco labore qui sit ut aliqua tempor consequat pariatur proident.",
@ -208,7 +178,13 @@ def main():
"nostrud" "nostrud"
] ]
}\ }\
""")) """)
def main():
app = QApplication(sys.argv)
ex = EditorAll()
ex.setWindowTitle(__file__)
ex.setText(EXAMPLE_TEXT)
ex.resize(800, 600) ex.resize(800, 600)
ex.show() ex.show()
sys.exit(app.exec_()) sys.exit(app.exec_())