mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 17:58:20 +00:00
black ifcopenshell-python
This commit is contained in:
@@ -25,47 +25,63 @@ import itertools
|
||||
|
||||
from pyparsing import *
|
||||
|
||||
try: from functools import reduce
|
||||
except: pass
|
||||
try:
|
||||
from functools import reduce
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
class Expression:
|
||||
def __init__(self, contents):
|
||||
self.contents = contents[0]
|
||||
|
||||
def __repr__(self):
|
||||
if self.op is None: return repr(self.contents)
|
||||
c = [isinstance(c,str) and c or str(c) for c in self.contents]
|
||||
if "%s" in self.op: return self.op % (" ".join(c))
|
||||
else: return "(%s)" % (" %s "%self.op).join(c)
|
||||
if self.op is None:
|
||||
return repr(self.contents)
|
||||
c = [isinstance(c, str) and c or str(c) for c in self.contents]
|
||||
if "%s" in self.op:
|
||||
return self.op % (" ".join(c))
|
||||
else:
|
||||
return "(%s)" % (" %s " % self.op).join(c)
|
||||
|
||||
def __iter__(self):
|
||||
return self.contents.__iter__()
|
||||
|
||||
|
||||
class Union(Expression):
|
||||
op = "|"
|
||||
|
||||
|
||||
class Concat(Expression):
|
||||
op = "+"
|
||||
|
||||
|
||||
class Optional(Expression):
|
||||
op = "Optional(%s)"
|
||||
|
||||
|
||||
class Repeated(Expression):
|
||||
op = "ZeroOrMore(%s)"
|
||||
|
||||
|
||||
class Term(Expression):
|
||||
op = None
|
||||
|
||||
|
||||
class Keyword:
|
||||
def __init__(self, contents):
|
||||
self.contents = contents[0]
|
||||
|
||||
def __repr__(self):
|
||||
return self.contents
|
||||
|
||||
|
||||
class Terminal:
|
||||
def __init__(self, contents):
|
||||
self.contents = contents[0]
|
||||
s = self.contents
|
||||
self.is_keyword = len(s) >= 4 and s[0::len(s)-1] == '""' and \
|
||||
all(c in alphanums+"_" for c in s[1:-1])
|
||||
self.is_keyword = len(s) >= 4 and s[0 :: len(s) - 1] == '""' and all(c in alphanums + "_" for c in s[1:-1])
|
||||
|
||||
def __repr__(self):
|
||||
ty = "CaselessKeyword" if self.is_keyword else "CaselessLiteral"
|
||||
return "%s(%s)" % (ty, self.contents)
|
||||
@@ -78,31 +94,33 @@ RBRACK = Suppress("]")
|
||||
LBRACE = Suppress("{")
|
||||
RBRACE = Suppress("}")
|
||||
EQUALS = Suppress("=")
|
||||
VBAR = Suppress("|")
|
||||
VBAR = Suppress("|")
|
||||
PERIOD = Suppress(".")
|
||||
HASH = Suppress("#")
|
||||
HASH = Suppress("#")
|
||||
|
||||
identifier = Word(alphanums+"_")
|
||||
keyword = Word(alphanums+"_").setParseAction(Keyword)
|
||||
identifier = Word(alphanums + "_")
|
||||
keyword = Word(alphanums + "_").setParseAction(Keyword)
|
||||
expression = Forward()
|
||||
optional = Group(LBRACK + expression + RBRACK).setParseAction(Optional)
|
||||
repeated = Group(LBRACE + expression + RBRACE).setParseAction(Repeated)
|
||||
terminal = quotedString.setParseAction(Terminal)
|
||||
term = (keyword | terminal | optional | repeated | (LPAREN + expression + RPAREN)).setParseAction(Term)
|
||||
concat = Group(term + OneOrMore(term)).setParseAction(Concat)
|
||||
factor = concat | term
|
||||
union = Group(factor + OneOrMore(VBAR + factor)).setParseAction(Union)
|
||||
rule = identifier + EQUALS + expression + PERIOD
|
||||
optional = Group(LBRACK + expression + RBRACK).setParseAction(Optional)
|
||||
repeated = Group(LBRACE + expression + RBRACE).setParseAction(Repeated)
|
||||
terminal = quotedString.setParseAction(Terminal)
|
||||
term = (keyword | terminal | optional | repeated | (LPAREN + expression + RPAREN)).setParseAction(Term)
|
||||
concat = Group(term + OneOrMore(term)).setParseAction(Concat)
|
||||
factor = concat | term
|
||||
union = Group(factor + OneOrMore(VBAR + factor)).setParseAction(Union)
|
||||
rule = identifier + EQUALS + expression + PERIOD
|
||||
|
||||
expression << (union | factor)
|
||||
|
||||
grammar = OneOrMore(Group(rule))
|
||||
grammar.ignore(HASH + restOfLine)
|
||||
|
||||
express = grammar.parseFile(os.path.join(os.path.dirname(__file__), 'express.bnf'))
|
||||
express = grammar.parseFile(os.path.join(os.path.dirname(__file__), "express.bnf"))
|
||||
|
||||
def find_bytype(expr, ty, li = None):
|
||||
if li is None: li = []
|
||||
|
||||
def find_bytype(expr, ty, li=None):
|
||||
if li is None:
|
||||
li = []
|
||||
if isinstance(expr, Term):
|
||||
expr = expr.contents
|
||||
if isinstance(expr, ty):
|
||||
@@ -113,34 +131,35 @@ def find_bytype(expr, ty, li = None):
|
||||
find_bytype(term, ty, li)
|
||||
return set(li)
|
||||
|
||||
|
||||
actions = {
|
||||
'type_decl' : "TypeDeclaration",
|
||||
'entity_decl' : "EntityDeclaration",
|
||||
'enumeration_type' : "EnumerationType",
|
||||
'aggregation_types' : "AggregationType",
|
||||
'general_aggregation_types' : "AggregationType",
|
||||
'select_type' : "SelectType",
|
||||
'binary_type' : "BinaryType",
|
||||
'subtype_declaration' : "SubTypeExpression",
|
||||
'supertype_constraint' : "SuperTypeExpression",
|
||||
'derive_clause' : "AttributeList",
|
||||
'inverse_clause' : "AttributeList",
|
||||
'inverse_attr' : "InverseAttribute",
|
||||
'bound_spec' : "BoundSpecification",
|
||||
'explicit_attr' : "ExplicitAttribute",
|
||||
'width_spec' : "WidthSpec",
|
||||
'string_type' : "StringType",
|
||||
'named_types' : "NamedType",
|
||||
'simple_types' : "SimpleType",
|
||||
"type_decl": "TypeDeclaration",
|
||||
"entity_decl": "EntityDeclaration",
|
||||
"enumeration_type": "EnumerationType",
|
||||
"aggregation_types": "AggregationType",
|
||||
"general_aggregation_types": "AggregationType",
|
||||
"select_type": "SelectType",
|
||||
"binary_type": "BinaryType",
|
||||
"subtype_declaration": "SubTypeExpression",
|
||||
"supertype_constraint": "SuperTypeExpression",
|
||||
"derive_clause": "AttributeList",
|
||||
"inverse_clause": "AttributeList",
|
||||
"inverse_attr": "InverseAttribute",
|
||||
"bound_spec": "BoundSpecification",
|
||||
"explicit_attr": "ExplicitAttribute",
|
||||
"width_spec": "WidthSpec",
|
||||
"string_type": "StringType",
|
||||
"named_types": "NamedType",
|
||||
"simple_types": "SimpleType",
|
||||
}
|
||||
|
||||
to_emit = set(id for id, expr in express)
|
||||
emitted = set()
|
||||
to_combine = set(["simple_id"])
|
||||
statements = []
|
||||
|
||||
terminals = reduce(lambda x,y: x | y, (find_bytype(e, Terminal) for id, e in express))
|
||||
keywords = list(filter(operator.attrgetter('is_keyword'), terminals))
|
||||
|
||||
terminals = reduce(lambda x, y: x | y, (find_bytype(e, Terminal) for id, e in express))
|
||||
keywords = list(filter(operator.attrgetter("is_keyword"), terminals))
|
||||
negated_keywords = map(lambda s: "~%s" % s, keywords)
|
||||
no_action = {"letter", "digit", "digits", "real_literal", "integer_literal"}
|
||||
|
||||
@@ -157,14 +176,15 @@ while True:
|
||||
stmt = " + ".join(itertools.chain(negated_keywords, ("originalTextFor(Combine%s)" % stmt,)))
|
||||
if id not in no_action and not isinstance(expr.contents, Keyword) and not id in to_combine:
|
||||
node_type = "ListNode" if "ZeroOrMore" in stmt else "Node"
|
||||
action = actions.get(id, "lambda s, loc, t: %s(s, loc, t, rule=\"%s\")" % (node_type, id))
|
||||
action = actions.get(id, 'lambda s, loc, t: %s(s, loc, t, rule="%s")' % (node_type, id))
|
||||
stmt = "%s.setParseAction(%s)" % (stmt, action)
|
||||
statements.append("%s = %s(\"%s\")" % (id, stmt, id))
|
||||
statements.append('%s = %s("%s")' % (id, stmt, id))
|
||||
to_emit -= emitted_in_loop
|
||||
if not emitted_in_loop: break
|
||||
if not emitted_in_loop:
|
||||
break
|
||||
|
||||
for id in to_emit:
|
||||
statements.append("%s = Forward()(\"%s\")" % (id, id))
|
||||
statements.append('%s = Forward()("%s")' % (id, id))
|
||||
|
||||
for id in to_emit:
|
||||
expr = [e for k, e in express if k == id][0]
|
||||
@@ -173,11 +193,14 @@ for id in to_emit:
|
||||
stmt = "Suppress%s" % stmt
|
||||
if id not in no_action and not isinstance(expr.contents, Keyword):
|
||||
node_type = "ListNode" if "ZeroOrMore" in stmt else "Node"
|
||||
action = ".setParseAction(%s)" % (actions[id] if id in actions else "lambda s, loc, t: %s(s, loc, t, rule=\"%s\")" % (node_type, id))
|
||||
action = ".setParseAction(%s)" % (
|
||||
actions[id] if id in actions else 'lambda s, loc, t: %s(s, loc, t, rule="%s")' % (node_type, id)
|
||||
)
|
||||
stmt = "(%s)%s" % (stmt, action)
|
||||
statements.append("%s << %s" % (id, stmt))
|
||||
|
||||
print ("""
|
||||
print(
|
||||
"""
|
||||
# This file is generated by IfcOpenShell ifcexpressparser bootstrap.py
|
||||
|
||||
import os
|
||||
@@ -215,4 +238,6 @@ if __name__ == "__main__":
|
||||
mdl = importlib.import_module(output)
|
||||
mdl.Generator(m).emit()
|
||||
sys.stdout.write(m.schema.name)
|
||||
"""%('\n '.join(statements)))
|
||||
"""
|
||||
% ("\n ".join(statements))
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user