Skip to content

Instantly share code, notes, and snippets.

@hadware hadware/
Last active Sep 24, 2019

What would you like to do?
Parsing Examples with sly
from sly import Lexer, Parser
import pprint
class JSONLexer(Lexer):
tokens = {"FLOAT", "INTEGER", "STRING"}
literals = {'{', '}', '[', ']', ',', ':'}
ignore = " \t\n"
def STRING(self, t):
t.value = t.value.strip("\"")
return t
def FLOAT(self, t):
t.value = float(t.value)
return t
def INTEGER(self, t):
t.value = int(t.value)
return t
class JSONParser(Parser):
tokens = JSONLexer.tokens
start = "json"
def json(self, p):
return p[0]
@_('"{" members "}"')
def object(self, p):
return {key: value for key, value in p.members}
def members(self, p):
return [p.pair]
@_('pair "," members')
def members(self, p):
return [p.pair] + p.members
@_('STRING ":" value')
def pair(self, p):
return p.STRING, p.value
@_('"[" elements "]"')
def array(self, p):
return p.elements
def elements(self, p):
return [p.value]
@_('value "," elements')
def elements(self, p):
return [p.value] + p.elements
def value(self, p):
return p[0]
def error(self, p):
raise ValueError("Parsing error at token %s" % str(p))
if __name__ == "__main__":
lexer = JSONLexer()
parser = JSONParser()
json_text = """{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
"delay" : 1.5
result = parser.parse(lexer.tokenize(json_text))
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import pprint
from dataclasses import dataclass
from typing import List, Tuple
from sly import Lexer, Parser
class Vertex:
coords: Tuple[float, float, float]
def __repr__(self):
return "VERTEX(%f,%f,%f)" % self.coords
class Facet:
vertices: Tuple[Vertex, Vertex, Vertex]
normal: Vertex
def __repr__(self):
return "\t FACET({normal})\n\t\t{vertices}".format(
vertices="\n\t\t".join(repr(vertex) for vertex in self.vertices)
class Solid:
name: str
facets: List[Facet]
def __repr__(self):
return "SOLID({name})\n{facets}\n".format(,
facets="\n".join(repr(facet) for facet in self.facets)
class STLLexer(Lexer):
tokens = {"SOLID_START", "SOLID_END",
@_(r"[ \t\n]+")
def ignore_whitespace(self, t):
self.lineno += t.value.count("\n")
SOLID_START = r"solid"
SOLID_END = r"endsolid"
FACET_START = r"facet normal"
FACET_END = r"endfacet"
LOOP_START = r"outer loop"
LOOP_END = r"endloop"
VERTEX_TAG = r"vertex"
def FLOAT(self, t):
t.value = float(t.value)
return t
class STLParser(Parser):
tokens = STLLexer.tokens
start = "stl"
def stl(self, p):
return p.solid
def solid(self, p):
return Solid(name=p[1], facets=p.facets_list)
def facets_list(self, p):
return [p.facet]
@_("facet facets_list")
def facets_list(self, p):
return [p.facet] + p.facets_list
@_("FACET_START triplet loop FACET_END")
def facet(self, p):
return Facet(vertices=p.loop, normal=Vertex(p.triplet))
@_("LOOP_START vertex vertex vertex LOOP_END")
def loop(self, p):
return p[1], p[2], p[3]
@_("VERTEX_TAG triplet")
def vertex(self, p):
return Vertex(p.triplet)
def triplet(self, p):
return p[0], p[1], p[2]
def error(self, token):
if __name__ == "__main__":
lexer = STLLexer()
parser = STLParser()
with open("star.stl") as stl_file:
# pprint.pprint(list(lexer.tokenize(
solid = parser.parse(lexer.tokenize(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.