def xldf_append(localpath,cmd,tree): found = findnode(tree,cmd.findtext('where')) if not found: print '[----] xldf:append failed: target id',cmd.findtext('where'),'not found' else: print '[XLDF] append(',cmd.findtext('where'),',', cx = 0 ctag = '' for p in cmd.findall('content/*'): if cx: if ctag==p.tag: cx += 1 else: if cx==1: print ctag.replace(slpsns.bgf_(''),''), else: print ctag.replace(slpsns.bgf_(''),''),'*',cx, cx = 1 ctag = p.tag else: cx = 1 ctag = p.tag #print found[-1].tag,found[-1][-1].tag if found[-1][-1].tag != 'content': found[-1].append(p) #found[-1].append(ET.Element('content',{})) else: found[-1][-1].append(p) if cx: if cx==1: print ctag.replace(slpsns.bgf_(''),''), else: print ctag.replace(slpsns.bgf_(''),''),'*',cx, print ')' return
def parse(self,prodelem): if prodelem.findall('label'): self.label = prodelem.findtext('label') else: self.label = '' self.nt = prodelem.findtext('nonterminal') self.expr = Expression(None) if len(prodelem.findall(slpsns.bgf_('expression')))!=1: print('Production of %s has more than one right hand side!' % self.nt) self.expr.parse(prodelem.findall(slpsns.bgf_('expression'))[0])
def abstr(e): #print('Visiting',e.tag) if e.tag == 'terminal': ne = ET.Element('marked') se = ET.SubElement(ne,slpsns.bgf_('expression')) se.append(e) return ne if e.tag in ('nonterminal','epsilon','empty','any','value'): return e if e.tag == 'selectable': return abstr(e.findall(slpsns.bgf_('expression'))[0].findall('*')[0]) ne = ET.Element(e.tag) for child in e.findall('*'): ne.append(abstr(child)) return ne
def anon(e): #print('Visiting',e.tag) if e.tag in ('terminal','nonterminal','epsilon','empty','any','value'): return e if e.tag == 'selectable': ne = ET.Element('marked') se = ET.SubElement(ne,slpsns.bgf_('expression')) ie = ET.SubElement(se,'selectable') ET.SubElement(ie,'selector').text = e.findtext('selector') ie.append(anon(e.findall(slpsns.bgf_('expression'))[0])) return ne ne = ET.Element(e.tag) for child in e.findall('*'): ne.append(anon(child)) return ne
def getXml(self): #print 'Getting the XML of selectable',self.sel,'...' self.ex = ET.Element(slpsns.bgf_('expression')) self.xml = ET.SubElement(self.ex,'seplistplus') self.xml.append(self.item.getXml()) self.xml.append(self.sep.getXml()) return self.ex
def getXml(self): #print 'Getting the XML of selectable',self.sel,'...' self.ex = ET.Element(slpsns.bgf_('expression')) self.xml = ET.SubElement(self.ex, 'seplistplus') self.xml.append(self.item.getXml()) self.xml.append(self.sep.getXml()) return self.ex
def getXml(self): #print 'Getting the XML of selectable',self.sel,'...' self.ex = ET.Element(slpsns.bgf_('expression')) self.xml = ET.SubElement(self.ex,'labelled') ET.SubElement(self.xml,'label').text = self.lab self.xml.append(self.expr.getXml()) return self.ex
def getXml(self): #print 'Getting the XML of production...' self.ex = ET.Element(slpsns.bgf_('production')) if self.label != '': ET.SubElement(self.ex, 'label').text = self.label ET.SubElement(self.ex, 'nonterminal').text = str(self.nt) self.ex.append(self.expr.getXml()) return self.ex
def parse(self,prodelem): if prodelem.findall('label'): self.label = prodelem.findtext('label') else: self.label = '' self.nt = prodelem.findtext('nonterminal') self.expr = Expression(None) self.expr.parse(prodelem.findall(slpsns.bgf_('expression'))[0])
def getXml(self): #print 'Getting the XML of grammar...' self.ex = ET.Element(slpsns.bgf_('grammar')) for e in self.roots: ET.SubElement(self.ex, 'root').text = e for e in self.prods: self.ex.append(e.getXml()) return self.ex
def getXml(self): #print 'Getting the XML of grammar...' self.ex = ET.Element(slpsns.bgf_('grammar')) for e in self.roots: ET.SubElement(self.ex,'root').text = e for e in self.prods: self.ex.append(e.getXml()) return self.ex
def parse(self, prodelem): if prodelem.findall('label'): self.label = prodelem.findtext('label') else: self.label = '' self.nt = prodelem.findtext('nonterminal') self.expr = Expression(None) self.expr.parse(prodelem.findall(slpsns.bgf_('expression'))[0])
def getXml(self): #print 'Getting the XML of production...' self.ex = ET.Element(slpsns.bgf_('production')) if self.label != '': ET.SubElement(self.ex,'label').text = self.label ET.SubElement(self.ex,'nonterminal').text = str(self.nt) self.ex.append(self.expr.getXml()) return self.ex
def getXml(self): #print 'Getting the XML of selectable',self.sel,'...' self.ex = ET.Element(slpsns.bgf_('expression')) self.xml = ET.SubElement(self.ex,'selectable') ET.SubElement(self.xml,'selector').text = self.sel if self.expr: self.xml.append(self.expr.getXml()) # troubleshooting? return self.ex
def getXml(self): #print 'Getting the XML of selectable',self.sel,'...' self.ex = ET.Element(slpsns.bgf_('expression')) self.xml = ET.SubElement(self.ex, 'selectable') ET.SubElement(self.xml, 'selector').text = self.sel if self.expr: self.xml.append(self.expr.getXml()) # troubleshooting? return self.ex
def parse(self, fname): self.roots = [] self.prods = [] self.xml = ET.parse(fname) for e in self.xml.findall('root'): self.roots.append(e.text) for e in self.xml.findall(slpsns.bgf_('production')): prod = Production() prod.parse(e) self.prods.append(prod)
def parse(self, xml): self.parsebasic(xml) for ss in xml.findall('state'): for s in ss.attrib['src'].split(','): self.data[s] = [[],[]] for p in ss.findall(slpsns.bgf_('production')): xp = BGF3.Production() xp.parse(p) self.data[s][0].append(xp) self.data[s][1] = ss.findall('in/*')
def parse(self,fname): self.roots = [] self.prods = [] self.xml = ET.parse(fname) for e in self.xml.findall('root'): self.roots.append(e.text) for e in self.xml.findall(slpsns.bgf_('production')): prod = Production() prod.parse(e) self.prods.append(prod)
def xldf_transformGrammar(localpath,cmd,tree): root = ET.Element(slpsns.xbgf_('sequence'),{}) cx0 = 0 for rule in cmd.findall('*')[1:]: if rule.tag != 'context': root.append(rule) cx0 += 1 ET.ElementTree(root).write('xldf-tmp.xbgf') found = findnode(tree,cmd.findtext('target')) if not found: print '[----] xldf:transform failed: target id',cmd.findtext('target'),'not found' return realprods = [] contextprods = [] for p in found.findall('*/*/'+slpsns.bgf_('production')): realprods.append(p) for p in found.findall('*/'+slpsns.bgf_('production')): realprods.append(p) for c in cmd.findall('context'): f = findnode(tree,c.text) if not f: print '[----] xldf:transform failed: context target id',c.text,'not found' return for p in f.findall('*/*/'+slpsns.bgf_('production')): contextprods.append(p) root = ET.Element(slpsns.bgf_('grammar'),{}) for p in realprods: root.append(p) for p in contextprods: root.append(p) #print '[====]',len(realprods),'+',len(contextprods),'productions' ET.ElementTree(root).write('xldf-tmp.bgf') if os.system('xbgf xldf-tmp.xbgf xldf-tmp.bgf xldf-tmp-result.bgf | grep -v Loading | grep -v Saving'): print '[----] xldf:transform failed: error in XBGF' return try: gtree = ET.parse('xldf-tmp-result.bgf') except IOError,e: print '[----] xldf:transform failed: XBGF result file not found' sys.exit(3) return
def getXml(self): #print 'Getting the XML of choice...' if len(self.data) == 0: return Empty().getXml() elif len(self.data) == 1: return self.data[0].getXml() else: self.ex = ET.Element(slpsns.bgf_('expression')) self.xml = ET.SubElement(self.ex,'choice') for el in self.data: self.xml.append(el.getXml()) return self.ex
def parse(self,ee): self.name = ee.tag for e in e.findall(slpsns.bgf_('*')): if e.tag == 'expression': ne = BGF3.Expression() elif e.tag == 'production': ne = BGF3.Production() else: print('Unknown parameter of type',e.tag) ne = None ne.parse(e) self.params.append(ne)
def parse(self, ee): self.name = ee.tag for e in e.findall(slpsns.bgf_('*')): if e.tag == 'expression': ne = BGF3.Expression() elif e.tag == 'production': ne = BGF3.Production() else: print('Unknown parameter of type', e.tag) ne = None ne.parse(e) self.params.append(ne)
def getXml(self): #print 'Getting the XML of choice...' if len(self.data) == 0: return Empty().getXml() elif len(self.data) == 1: return self.data[0].getXml() else: self.ex = ET.Element(slpsns.bgf_('expression')) self.xml = ET.SubElement(self.ex, 'choice') for el in self.data: self.xml.append(el.getXml()) return self.ex
def getXml(self): #print 'Getting the XML of nonterminal',self.data,'...' self.ex = ET.Element(slpsns.bgf_('expression')) ET.SubElement(self.ex,'nonterminal').text = self.data return self.ex
def getXml(self): #print 'Getting the XML of value...' self.ex = ET.Element(slpsns.bgf_('expression')) ET.SubElement(self.ex, 'value').text = self.data return self.ex
def getXml(self): #print 'Getting the XML of empty...' self.ex = ET.Element(slpsns.bgf_('expression')) ET.SubElement(self.ex, 'empty') return self.ex
def getXml(self): #print 'Getting the XML of empty...' self.ex = ET.Element(slpsns.bgf_('expression')) ET.SubElement(self.ex,'empty') return self.ex
def parse(self, selelem): self.sel = selelem.findtext('selector') self.expr = Expression(None) self.expr.parse(selelem.findall(slpsns.bgf_('expression'))[0])
def parse(self, slelem): exprs = slelem.findall(slpsns.bgf_('expression')) self.item = Expression(None) self.item.parse(exprs[0]) self.sep = Expression(None) self.sep.parse(exprs[1])
def parse(self,selelem): self.sel = selelem.findtext('selector') self.expr = Expression(None) self.expr.parse(selelem.findall(slpsns.bgf_('expression'))[0])
def __init__(self, xml): self.nt = xml.findtext('state/'+slpsns.bgf_('production')+'/nonterminal') self.parse(xml)
def main(xsdfile,bgffile,ldffile): grammar={} gtree = ET.parse(bgffile) stree = ET.parse(xsdfile) dtree = ET.Element('ldf:document') cx=0 for prod in gtree.findall('//'+slpsns.bgf_('production')): cx+=1 nt = prod.findtext('nonterminal') if nt not in grammar.keys(): grammar[nt] = [] grammar[nt].append(prod) print 'Found', cx, 'productions' dtree.set('xmlns:ldf',slpsns.ldfns) dtree.set('xmlns:bgf',slpsns.bgfns) dtree.set('xmlns:html',slpsns.htmlns) section = ET.SubElement(dtree,'titlePage') el = ET.SubElement(section,'author') el.text = 'XSD2LDF generator' el = ET.SubElement(section,'topic') el.text = stree.findall('/'+slpsns.xsd_('annotation')+'/'+slpsns.xsd_('documentation'))[0].text el = ET.SubElement(section,'version') el.text = '1.0' el = ET.SubElement(section,'status') el.text = 'unknown' el = ET.SubElement(section,'date') # generate!!! el.text = '2008-08-01' section = ET.SubElement(dtree,'frontMatter') el = ET.SubElement(section,'foreword') el = ET.SubElement(el,'content') for p in stree.findall('/'+slpsns.xsd_('annotation')+'/'+slpsns.xsd_('documentation'))[1:]: pel = ET.SubElement(el,'text') pel.text = p.text if not el.findall('*'): ET.SubElement(el,'empty') if stree.findall('/'+slpsns.xsd_('import')): el = ET.SubElement(section,'normativeReferences') el = ET.SubElement(el,'content') el = ET.SubElement(el,'list') part = ET.SubElement(dtree,'part') for p in stree.findall('/'+slpsns.xsd_('import')): pel = ET.SubElement(el,'item') pel.text = p.attrib['schemaLocation'] istree = ET.parse('/'.join(xsdfile.split('/')[:-1])+'/'+p.attrib['schemaLocation']) mapXSD2LDF(istree,part,grammar) print len(stree.findall('/'+slpsns.xsd_('import'))),'external schema(ta) imported.' if dtree.findall('part'): part = dtree.findall('part')[0] else: part = ET.SubElement(dtree,'part') mapXSD2LDF(stree,part,grammar) # bit of normalisation fullTree = ET.ElementTree(dtree) for e in fullTree.findall('//content'): if not len(e): print '[++++] Empty content made explicit.' ET.SubElement(e,'empty') # serialise! fullTree.write(ldffile) return
def getXml(self): #print 'Getting the XML of +...' self.ex = ET.Element(slpsns.bgf_('expression')) ET.SubElement(self.ex, 'plus').append(self.data.getXml()) return self.ex
def parse(self,labelem): self.lab = labelem.findtext('label') self.expr = Expression(None) self.expr.parse(labelem.findall(slpsns.bgf_('expression'))[0])
root.append(p) #print '[====]',len(realprods),'+',len(contextprods),'productions' ET.ElementTree(root).write('xldf-tmp.bgf') if os.system('xbgf xldf-tmp.xbgf xldf-tmp.bgf xldf-tmp-result.bgf | grep -v Loading | grep -v Saving'): print '[----] xldf:transform failed: error in XBGF' return try: gtree = ET.parse('xldf-tmp-result.bgf') except IOError,e: print '[----] xldf:transform failed: XBGF result file not found' sys.exit(3) return # remove old production cx1 = 0 if found[-1][-1].tag != 'content': for p in found[-1].findall(slpsns.bgf_('production')): found[-1].remove(p) cx1 +=1 else: for p in found[-1][-1].findall(slpsns.bgf_('production')): found[-1][-1].remove(p) cx1 +=1 # add new productions cx2 = 0 for p in gtree.findall(slpsns.bgf_('production')): isContext = False for cp in contextprods: if xml_eq(cp,p): isContext = True if isContext: continue
def getXml(self): #print 'Getting the XML of value...' self.ex = ET.Element(slpsns.bgf_('expression')) ET.SubElement(self.ex,'value').text = self.data return self.ex
def getXml(self): #print 'Getting the XML of nonterminal',self.data,'...' self.ex = ET.Element(slpsns.bgf_('expression')) ET.SubElement(self.ex, 'nonterminal').text = self.data return self.ex
print('xedd:introduce-metasymbol('+ms+', '+quoted(x)+', '+quoted(y)+');') ms1 = 'start-'+ms+'-symbol' ms2 = 'end-'+ms+'-symbol' if ms1 in config.keys(): print('Cannot introduce metasymbol that is already defined: %s.' % ms1) sys.exit(1) elif ms2 in config.keys(): print('Cannot introduce metasymbol that is already defined: %s.' % ms2) sys.exit(1) else: config[ms1] = x config[ms2] = y if ms == 'group': p = ET.SubElement(cbgf,slpsns.cbgf_('add-remove')) p = ET.SubElement(p,'vertical') p = ET.SubElement(p,slpsns.bgf_('production')) ET.SubElement(p,'label').text = ms ET.SubElement(p,'nonterminal').text = edd2 + 'Symbol' e = ET.SubElement(p,slpsns.bgf_('expression')) s = ET.SubElement(e,'sequence') ET.SubElement(ET.SubElement(s,slpsns.bgf_('expression')),'terminal').text = x if 'definition-separator-symbol' in config: slp = ET.SubElement(ET.SubElement(s,slpsns.bgf_('expression')),'seplistplus') ET.SubElement(ET.SubElement(slp,slpsns.bgf_('expression')),'nonterminal').text = edd2 + 'Definition' ET.SubElement(ET.SubElement(slp,slpsns.bgf_('expression')),'terminal').text = config['definition-separator-symbol'] else: ET.SubElement(ET.SubElement(s,slpsns.bgf_('expression')),'nonterminal').text = edd2 + 'Definition' ET.SubElement(ET.SubElement(s,slpsns.bgf_('expression')),'terminal').text = y elif ms in ('seplist-plus','seplist-star'): p = ET.SubElement(cbgf,slpsns.cbgf_('add-remove')) p = ET.SubElement(p,'vertical')
def __init__(self, xml): self.expr = BGF3.Expression([]) self.expr.parse(xml.findall(slpsns.bgf_('expression'))[0]) # apply namemap!!! self.parse(xml) self.scope = xml.findall('in')
def getXml(self): #print 'Getting the XML of +...' self.ex = ET.Element(slpsns.bgf_('expression')) ET.SubElement(self.ex,'plus').append(self.data.getXml()) return self.ex
def parse(self,slelem): exprs = slelem.findall(slpsns.bgf_('expression')) self.item = Expression(None) self.item.parse(exprs[0]) self.sep = Expression(None) self.sep.parse(exprs[1])
def main(xsdfile, bgffile, ldffile): grammar = {} gtree = ET.parse(bgffile) stree = ET.parse(xsdfile) dtree = ET.Element('ldf:document') cx = 0 for prod in gtree.findall('//' + slpsns.bgf_('production')): cx += 1 nt = prod.findtext('nonterminal') if nt not in grammar.keys(): grammar[nt] = [] grammar[nt].append(prod) print 'Found', cx, 'productions' dtree.set('xmlns:ldf', slpsns.ldfns) dtree.set('xmlns:bgf', slpsns.bgfns) dtree.set('xmlns:html', slpsns.htmlns) section = ET.SubElement(dtree, 'titlePage') el = ET.SubElement(section, 'author') el.text = 'XSD2LDF generator' el = ET.SubElement(section, 'topic') el.text = stree.findall('/' + slpsns.xsd_('annotation') + '/' + slpsns.xsd_('documentation'))[0].text el = ET.SubElement(section, 'version') el.text = '1.0' el = ET.SubElement(section, 'status') el.text = 'unknown' el = ET.SubElement(section, 'date') # generate!!! el.text = '2008-08-01' section = ET.SubElement(dtree, 'frontMatter') el = ET.SubElement(section, 'foreword') el = ET.SubElement(el, 'content') for p in stree.findall('/' + slpsns.xsd_('annotation') + '/' + slpsns.xsd_('documentation'))[1:]: pel = ET.SubElement(el, 'text') pel.text = p.text if not el.findall('*'): ET.SubElement(el, 'empty') if stree.findall('/' + slpsns.xsd_('import')): el = ET.SubElement(section, 'normativeReferences') el = ET.SubElement(el, 'content') el = ET.SubElement(el, 'list') part = ET.SubElement(dtree, 'part') for p in stree.findall('/' + slpsns.xsd_('import')): pel = ET.SubElement(el, 'item') pel.text = p.attrib['schemaLocation'] istree = ET.parse('/'.join(xsdfile.split('/')[:-1]) + '/' + p.attrib['schemaLocation']) mapXSD2LDF(istree, part, grammar) print len(stree.findall( '/' + slpsns.xsd_('import'))), 'external schema(ta) imported.' if dtree.findall('part'): part = dtree.findall('part')[0] else: part = ET.SubElement(dtree, 'part') mapXSD2LDF(stree, part, grammar) # bit of normalisation fullTree = ET.ElementTree(dtree) for e in fullTree.findall('//content'): if not len(e): print '[++++] Empty content made explicit.' ET.SubElement(e, 'empty') # serialise! fullTree.write(ldffile) return