mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 01:52:56 +01:00
grammar.py et al: Centralize more code
More code is removed from the special parser directories and centralized into grammar.py, Cmd.py, and generate-flex-bison.mk. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
fee94deb48
commit
6297f10f55
6 changed files with 210 additions and 177 deletions
|
|
@ -1,79 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import print_function
|
||||
import argparse
|
||||
import sys
|
||||
import re
|
||||
import textwrap
|
||||
from collections import OrderedDict
|
||||
from abc import abstractmethod
|
||||
|
||||
import jwutils
|
||||
|
||||
from jwutils.log import *
|
||||
from jwutils import grammar
|
||||
|
||||
base = 'grammartest'
|
||||
mip = '_JW_PYTHON_' + base + base.upper()
|
||||
namespace = base
|
||||
|
||||
def create_grammartest_ebnf(grammar):
|
||||
print(jwutils.grammar.create_ebnf(grammar))
|
||||
|
||||
def create_grammartest_y(grammar):
|
||||
print(jwutils.grammar.create_yacc(grammar))
|
||||
|
||||
def create_grammartest_l(grammar):
|
||||
print(jwutils.grammar.create_lex(grammar))
|
||||
|
||||
def create_include_grammartest_h(grammar):
|
||||
print(jwutils.grammar.create_header(grammar, mip=mip, namespace=namespace))
|
||||
|
||||
class GrammarCmd(jwutils.grammar.GrammarCmd):
|
||||
|
||||
def __init__(self, name, help):
|
||||
super(GrammarCmd, self).__init__(name, help=help)
|
||||
|
||||
@abstractmethod
|
||||
def _run(self, grammar):
|
||||
pass
|
||||
|
||||
def add_parser(self, parsers):
|
||||
p = super(GrammarCmd, self).add_parser(parsers)
|
||||
return p
|
||||
|
||||
def run(self, args):
|
||||
with open(args.input, 'r') as infile:
|
||||
contents = infile.read()
|
||||
grammar = jwutils.grammar.grammar_parse_ebnf(contents)
|
||||
grammar = super(GrammarCmd, self).processGrammar(args, grammar)
|
||||
self._run(args, grammar)
|
||||
|
||||
class CmdCreate(GrammarCmd):
|
||||
|
||||
def __init__(self):
|
||||
super(CmdCreate, self).__init__("create", help="Create a file")
|
||||
|
||||
def add_parser(self, parsers):
|
||||
p = super(CmdCreate, self).add_parser(parsers)
|
||||
p.add_argument("output", help="output file")
|
||||
return p
|
||||
|
||||
def _run(self, args, grammar):
|
||||
cmd = getattr(sys.modules[__name__], 'create_' + re.sub(r'[-./]', '_', args.output))
|
||||
cmd(grammar)
|
||||
|
||||
class CmdCheck(GrammarCmd):
|
||||
|
||||
def __init__(self):
|
||||
super(CmdCheck, self).__init__("check", help="Check grammar")
|
||||
|
||||
def add_parser(self, parsers):
|
||||
p = super(CmdCheck, self).add_parser(parsers)
|
||||
return p
|
||||
|
||||
def _run(self, args, grammar):
|
||||
pass
|
||||
|
||||
jwutils.run_sub_commands('generate Test parser files')
|
||||
jwutils.run_sub_commands('generate Test parser files', modules = ['jwutils.grammar'])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue