|
| 1 | +############################################################################### |
| 2 | +# # |
| 3 | +# This file is part of IfcOpenShell. # |
| 4 | +# # |
| 5 | +# IfcOpenShell is free software: you can redistribute it and/or modify # |
| 6 | +# it under the terms of the Lesser GNU General Public License as published by # |
| 7 | +# the Free Software Foundation, either version 3.0 of the License, or # |
| 8 | +# (at your option) any later version. # |
| 9 | +# # |
| 10 | +# IfcOpenShell is distributed in the hope that it will be useful, # |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 13 | +# Lesser GNU General Public License for more details. # |
| 14 | +# # |
| 15 | +# You should have received a copy of the Lesser GNU General Public License # |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. # |
| 17 | +# # |
| 18 | +############################################################################### |
| 19 | + |
| 20 | +import sys |
| 21 | +import string |
| 22 | +from pyparsing import * |
| 23 | + |
| 24 | +class Expression: |
| 25 | + def __init__(self, contents): |
| 26 | + self.contents = contents[0] |
| 27 | + def __repr__(self): |
| 28 | + if self.op is None: return repr(self.contents) |
| 29 | + c = [isinstance(c,str) and c or str(c) for c in self.contents] |
| 30 | + if "%s" in self.op: return self.op % (" ".join(c)) |
| 31 | + else: return "(%s)" % (" %s "%self.op).join(c) |
| 32 | + def __iter__(self): |
| 33 | + return self.contents.__iter__() |
| 34 | + |
| 35 | +class Union(Expression): |
| 36 | + op = "|" |
| 37 | + |
| 38 | +class Concat(Expression): |
| 39 | + op = "+" |
| 40 | + |
| 41 | +class Optional(Expression): |
| 42 | + op = "Optional(%s)" |
| 43 | + |
| 44 | +class Repeated(Expression): |
| 45 | + op = "ZeroOrMore(%s)" |
| 46 | + |
| 47 | +class Term(Expression): |
| 48 | + op = None |
| 49 | + |
| 50 | +class Keyword: |
| 51 | + def __init__(self, contents): |
| 52 | + self.contents = contents[0] |
| 53 | + def __repr__(self): |
| 54 | + return self.contents |
| 55 | + |
| 56 | +class Terminal: |
| 57 | + def __init__(self, contents): |
| 58 | + self.contents = contents[0] |
| 59 | + def __repr__(self): |
| 60 | + s = self.contents |
| 61 | + is_keyword = len(s) >= 4 and s[0::len(s)-1] == '""' and \ |
| 62 | + all(c in alphanums+"_" for c in s[1:-1]) |
| 63 | + ty = "CaselessKeyword" if is_keyword else "CaselessLiteral" |
| 64 | + return "%s(%s)" % (ty, s) |
| 65 | + |
| 66 | + |
| 67 | +LPAREN = Suppress("(") |
| 68 | +RPAREN = Suppress(")") |
| 69 | +LBRACK = Suppress("[") |
| 70 | +RBRACK = Suppress("]") |
| 71 | +LBRACE = Suppress("{") |
| 72 | +RBRACE = Suppress("}") |
| 73 | +EQUALS = Suppress("=") |
| 74 | +VBAR = Suppress("|") |
| 75 | +PERIOD = Suppress(".") |
| 76 | +HASH = Suppress("#") |
| 77 | + |
| 78 | +identifier = Word(alphanums+"_") |
| 79 | +keyword = Word(alphanums+"_").setParseAction(Keyword) |
| 80 | +expression = Forward() |
| 81 | +optional = Group(LBRACK + expression + RBRACK).setParseAction(Optional) |
| 82 | +repeated = Group(LBRACE + expression + RBRACE).setParseAction(Repeated) |
| 83 | +terminal = quotedString.setParseAction(Terminal) |
| 84 | +term = (keyword | terminal | optional | repeated | (LPAREN + expression + RPAREN)).setParseAction(Term) |
| 85 | +concat = Group(term + OneOrMore(term)).setParseAction(Concat) |
| 86 | +factor = concat | term |
| 87 | +union = Group(factor + OneOrMore(VBAR + factor)).setParseAction(Union) |
| 88 | +rule = identifier + EQUALS + expression + PERIOD |
| 89 | + |
| 90 | +expression << (union | factor) |
| 91 | + |
| 92 | +grammar = OneOrMore(Group(rule)) |
| 93 | +grammar.ignore(HASH + restOfLine) |
| 94 | + |
| 95 | +express = grammar.parseFile(sys.argv[1]) |
| 96 | + |
| 97 | +def find_keywords(expr, li = None): |
| 98 | + if li is None: li = [] |
| 99 | + if isinstance(expr, Term): |
| 100 | + expr = expr.contents |
| 101 | + if isinstance(expr, Keyword): |
| 102 | + li.append(repr(expr)) |
| 103 | + return li |
| 104 | + elif isinstance(expr, Expression): |
| 105 | + for term in expr: |
| 106 | + find_keywords(term, li) |
| 107 | + return set(li) |
| 108 | + |
| 109 | +actions = { |
| 110 | + 'type_decl' : "lambda t: TypeDeclaration(t)", |
| 111 | + 'entity_decl' : "lambda t: EntityDeclaration(t)", |
| 112 | + 'underlying_type' : "lambda t: UnderlyingType(t)", |
| 113 | + 'enumeration_type' : "lambda t: EnumerationType(t)", |
| 114 | + 'aggregation_types' : "lambda t: AggregationType(t)", |
| 115 | + 'general_aggregation_types' : "lambda t: AggregationType(t)", |
| 116 | + 'select_type' : "lambda t: SelectType(t)", |
| 117 | + 'binary_type' : "lambda t: BinaryType(t)", |
| 118 | + 'subtype_declaration' : "lambda t: SubtypeExpression(t)", |
| 119 | + 'derive_clause' : "lambda t: AttributeList('derive', t)", |
| 120 | + 'derived_attr' : "lambda t: DerivedAttribute(t)", |
| 121 | + 'inverse_clause' : "lambda t: AttributeList('inverse', t)", |
| 122 | + 'inverse_attr' : "lambda t: InverseAttribute(t)", |
| 123 | + 'bound_spec' : "lambda t: BoundSpecification(t)", |
| 124 | + 'explicit_attr' : "lambda t: ExplicitAttribute(t)", |
| 125 | +} |
| 126 | + |
| 127 | +to_emit = set(id for id, expr in express) |
| 128 | +emitted = set() |
| 129 | +to_combine = set(["simple_id"]) |
| 130 | +to_ignore = set(["where_clause", "supertype_constraint", "unique_clause"]) |
| 131 | +statements = [] |
| 132 | + |
| 133 | +while True: |
| 134 | + emitted_in_loop = set() |
| 135 | + for id, expr in express: |
| 136 | + kws = find_keywords(expr) |
| 137 | + found = [k in emitted for k in kws] |
| 138 | + if id in to_emit and all(found): |
| 139 | + emitted_in_loop.add(id) |
| 140 | + emitted.add(id) |
| 141 | + stmt = "(%s)" % expr |
| 142 | + if id in to_combine: |
| 143 | + stmt = "originalTextFor(Combine%s)" % stmt |
| 144 | + if id in actions: |
| 145 | + stmt = "%s.setParseAction(%s)" % (stmt, actions[id]) |
| 146 | + statements.append("%s = %s" % (id, stmt)) |
| 147 | + to_emit -= emitted_in_loop |
| 148 | + if not emitted_in_loop: break |
| 149 | + |
| 150 | +for id in to_emit: |
| 151 | + action = ".setParseAction(%s)" % actions[id] if id in actions else "" |
| 152 | + statements.append("%s = Forward()%s" % (id, action)) |
| 153 | + |
| 154 | +for id in to_emit: |
| 155 | + expr = [e for k, e in express if k == id][0] |
| 156 | + stmt = "(%s)" % expr |
| 157 | + if id in to_combine: |
| 158 | + stmt = "Suppress%s" % stmt |
| 159 | + statements.append("%s << %s" % (id, stmt)) |
| 160 | + |
| 161 | +print ("""import sys |
| 162 | +from pyparsing import * |
| 163 | +from nodes import * |
| 164 | +
|
| 165 | +%s |
| 166 | +
|
| 167 | +import schema |
| 168 | +import mapping |
| 169 | +
|
| 170 | +import header |
| 171 | +import enum_header |
| 172 | +import implementation |
| 173 | +
|
| 174 | +syntax.ignore(Regex(r"\((?:\*(?:[^*]*\*+)+?\))")) |
| 175 | +ast = syntax.parseFile(sys.argv[1]) |
| 176 | +schema = schema.Schema(ast) |
| 177 | +mapping = mapping.Mapping(schema) |
| 178 | +
|
| 179 | +header.Header(mapping).emit() |
| 180 | +enum_header.EnumHeader(mapping).emit() |
| 181 | +implementation.Implementation(mapping).emit() |
| 182 | +"""%('\n'.join(statements))) |
0 commit comments