Skip to content

Commit

Permalink
Cleanups and get it working without old code
Browse files Browse the repository at this point in the history
  • Loading branch information
mhosken committed Jan 6, 2023
1 parent 4f88876 commit 471a151
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
25 changes: 13 additions & 12 deletions python/lib/usfmtc/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def terminal(self, e, res, skip, p, i):
self.back.add_terminal(n, v)

def attribute(self, e, res, skip, p, i):
res = self.back.attrib_start(self, e, res)
name = e.findtext(f"./{relaxns}name") or "*"
res = self.back.attrib_start(self, e, res, name)
g = int(e.get(f"{usfmns}grouping", 0))
self.groupings.append(g)
self.proc_children(e, res)
Expand All @@ -98,7 +99,8 @@ def attribute(self, e, res, skip, p, i):
return res

def element(self, e, res, skip, p, i):
res = self.back.elem_start(self, e, res)
name = e.findtext(f"./{relaxns}name")
res = self.back.elem_start(self, e, res, name)
g = int(e.get(f"{usfmns}grouping", 0))
self.groupings.append(g)
self.proc_children(e, res, False)
Expand Down Expand Up @@ -239,7 +241,8 @@ def proc_children(self, e, res):
return res

def attribute(self, e, res, p):
res = self.back.attrib_start(self, e, res)
name = e.findtext(f"./{relaxns}name") or "*"
res = self.back.attrib_start(self, e, res, name)
g = int(e.get(f"{usfmns}grouping", 0))
self.groupings.append(g)
self.proc_children(e, res)
Expand All @@ -248,11 +251,11 @@ def attribute(self, e, res, p):
return res

def element(self, e, res, p):
name = e.get(f"./{relaxns}name")
name = e.findtext(f"./{relaxns}name")
if name in self.elements:
res = self.elements[name]
else:
res = self.back.elem_start(self, e, res)
res = self.back.elem_start(self, e, res, name)
g = int(e.get(f"{usfmns}grouping", 0))
self.groupings.append(g)
self.proc_children(e, res)
Expand Down Expand Up @@ -326,6 +329,7 @@ def properties(self, e, res, p):
res.properties.update(e.attrib)
return res

# ----------------------------------------------------------------------------

import usfmtc.railroad as rr
CSS_STYLE = '''
Expand Down Expand Up @@ -365,7 +369,6 @@ def properties(self, e, res, p):
'''

class RailRoad:

def __init__(self):
self.terminals = {}

Expand Down Expand Up @@ -421,13 +424,13 @@ def output(self, top, **kw):
defaults.update(kw)
return rr.Diagram(top.asRail(), type='complex', css=CSS_STYLE.format(**defaults))

def attrib_start(self, parser, e, context):
def attrib_start(self, parser, e, context, name):
return self.append_seq(context, stacked=e.get("{}stacked".format(usfmns), "false") in ("true", "1"))

def attrib_end(self, parser, e, context):
return context

def elem_start(self, parser, e, context):
def elem_start(self, parser, e, context, name):
return self.append_seq(context, stacked=e.get("{}stacked".format(usfmns), "false") in ("true", "1"))

def elem_end(self, parser, e, context):
Expand Down Expand Up @@ -499,8 +502,7 @@ def backref(self, index, context=None):

class XMLRailRoad(RailRoad):

def elem_start(self, parser, e, context):
name = e.findtext(f'./{relaxns}name')
def elem_start(self, parser, e, context, name):
seq = self.append_type(context, rr.Sequence, forced=True)
context = self.match(f"<{name}>", seq)
res = self.append_type(seq, rr.Choice, 0, forced=True, noclose=1)
Expand All @@ -515,8 +517,7 @@ def elem_end(self, parser, e, context):
context.args[0] = len(context) - 1
return context

def attrib_start(self, parser, e, context):
name = e.findtext(f'./{relaxns}name') or "*"
def attrib_start(self, parser, e, context, name):
parent = context
while parent is not None and parent.kw.get('noclose', 0) == 0:
parent = parent.parent
Expand Down
42 changes: 13 additions & 29 deletions python/scripts/mkraildiagrams
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,26 @@

import os, sys, argparse
try:
from usfmtc.sfmrail import SFMRail, XMLRail
from usfmtc.parser import UsfmParser, XmlParser, RailRoad, XMLRailRoad
except ImportError:
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'lib'))
from usfmtc.sfmrail import SFMRail, XMLRail
from usfmtc.parserelax import ETDoc, Grammar, RelaxXMLParser, RelaxUSFMParser
from usfmtc.parser import UsfmParser, XmlParser, RailRoad, XMLRailRoad
from usfmtc.usxgrammar import alljobs
import xml.etree.ElementTree as et

parser = argparse.ArgumentParser()
parser.add_argument("-g","--grammar",required=True,help="USX grammar file")
parser.add_argument("-o","--outdir",required=True,help="Output directory for generated svgs")
parser.add_argument("-n","--node",help="Process a single node")
parser.add_argument("-C","--charwidth",type=float,default=6.,help="Average width of characters to use")
parser.add_argument("-z","--debug",type=int,default=0,help="bit 0 - print job name")
parser.add_argument("-A","--alt",action="store_true",help="Use alternative parser")
args = parser.parse_args()

rdoc = ETDoc(args.grammar)
if not args.alt:
from usfmtc.parser import UsfmParser, XmlParser, RailRoad, XMLRailRoad
backendsfm = RailRoad()
backendxml = XMLRailRoad()
sfmproc = UsfmParser(rdoc, backendsfm)
xmlproc = XmlParser(rdoc, backendxml)
else:
backendsfm = SFMRail()
rgram = Grammar(rdoc)
rgram.parse(rdoc.getroot())
backendxml = XMLRail()
rdoc = et.parse(args.grammar)
backendsfm = RailRoad()
backendxml = XMLRailRoad()
sfmproc = UsfmParser(rdoc, backendsfm)
xmlproc = XmlParser(rdoc, backendxml)

if args.node:
jobs = {args.node: alljobs[args.node]}
Expand All @@ -45,19 +37,11 @@ for k, j in jobs.items():
print(f"{j[0]}_{a[0]}")
#rdia = RDiagram(k, rdoc, keeps=keeps, cwidth=args.charwidth)
#rgram.makediagram(k, rdia=rdia, usfm=a[1])
if not args.alt:
back = backendsfm if a[1] else backendxml
proc = sfmproc if a[1] else xmlproc
else:
back = backendsfm if a[1] else backendxml
proc = RelaxXMLParser
if not args.alt:
p = proc
if p.parseRef(k, flattens=keeps) is None:
continue
else:
p = proc(rdoc, back, rgram, keeps)
rgram.parseRef(k, parser=p, quietRefs=False)
back = backendsfm if a[1] else backendxml
proc = sfmproc if a[1] else xmlproc
p = proc
if p.parseRef(k, flattens=keeps) is None:
continue
outfile = os.path.join(args.outdir, "{}_{}.svg".format(j[0], a[0]))
with open(outfile, "w", encoding="utf-8") as outf:
d = back.output(p.curr, color='rgb(210, 255, 210)' if a[1] else 'rgb(210, 210, 255)')
Expand Down

0 comments on commit 471a151

Please sign in to comment.