Example #1
0
class QuotedToken(modgrammar.Grammar):
    grammar_whitespace_mode = 'explicit'
    grammar = (modgrammar.LITERAL('"'), modgrammar.OPTIONAL(Token), modgrammar.ONE_OR_MORE(modgrammar.WHITESPACE, Token),
               modgrammar.LITERAL('"'))

    def grammar_elem_init(self, session_data):
        if self[1] is not None:
            value = self[1].string + self[2].string
        else:
            value = self[2].string

        self.model = model.Token(value=value)
Example #2
0
class NameDeclaration(modgrammar.Grammar):
    grammar_whitespace_mode = 'explicit'
    grammar = (modgrammar.LITERAL("grammar"), modgrammar.WHITESPACE,
               modgrammar.OPTIONAL(Package, modgrammar.LITERAL('.')),
               JavaIdentifier,
               modgrammar.LITERAL(";"))

    def grammar_elem_init(self, session_data):
        self.name = self[3].value

        if self[2] is None:
            self.package = None
        else:
            self.package = self[2][0].value
Example #3
0
class Rule(modgrammar.Grammar):
    grammar_whitespace_mode = 'optional'
    grammar = (modgrammar.OPTIONAL(PublicModifier),
               modgrammar.LITERAL("<"),
               JavaIdentifier,
               modgrammar.LITERAL(">"),
               modgrammar.LITERAL("="),
               RuleExpansion,
               modgrammar.LITERAL(";"))

    def grammar_elem_init(self, session_data):
        scope = model.Rule.SCOPE_PRIVATE

        if self[0] is not None:
            scope = model.Rule.SCOPE_PUBLIC

        self.model = model.Rule(name=self[2].value, value=self[5].model, scope=scope)
Example #4
0
class SelfIdentifyingHeader(modgrammar.Grammar):
    grammar_whitespace_mode = 'explicit'
    grammar = (modgrammar.LITERAL("#JSGF"), modgrammar.WHITESPACE,
               modgrammar.WORD("A-Za-z0-9._\-"),
               modgrammar.OPTIONAL(modgrammar.WHITESPACE, modgrammar.WORD("A-Za-z0-9._\-")),
               modgrammar.OPTIONAL(modgrammar.WHITESPACE, modgrammar.WORD("A-Za-z0-9._\-")),
               modgrammar.LITERAL(";"), modgrammar.LITERAL('\n'))

    def grammar_elem_init(self, session_data):
        self.version = self[2].string

        if self[3] is not None:
            self.encoding = self[3][1].string
        else:
            self.encoding = None

        if self[4] is not None:
            self.locale = self[4][1].string
        else:
            self.locale = None
Example #5
0
def regularize(grammar):
    if hasattr(grammar, 'grammar_parse'):
        return (grammar,)
    if isinstance(grammar, str):
        return (modgrammar.LITERAL(grammar),)
    if grammar is None:
        return (modgrammar.EMPTY,)
    try:
        result = []
        for g in grammar:
            result.extend(regularize(g))
        return tuple(result)
    except TypeError:
        raise modgrammar.GrammarDefError(
            "object of type '%s' cannot be converted to Grammar" % (type(grammar).__name__,))
Example #6
0
class ImportStatement(modgrammar.Grammar):
    grammar_whitespace_mode = 'explicit'
    grammar = (
        modgrammar.LITERAL("import"), modgrammar.WHITESPACE, modgrammar.LITERAL("<"),
        Package,
        modgrammar.LITERAL('.'),
        JavaIdentifier | modgrammar.LITERAL('*'),
        modgrammar.LITERAL(">"),
        modgrammar.LITERAL(";"), modgrammar.OPTIONAL(modgrammar.WHITESPACE))

    def grammar_elem_init(self, session_data):
        self.package = self[3].value
        self.rule = self[5].string
Example #7
0
class PublicModifier(modgrammar.Grammar):
    grammar_whitespace_mode = 'optional'
    grammar = (modgrammar.LITERAL("public"))
Example #8
0
class AlternativeSeparator(modgrammar.Grammar):
    grammar_whitespace_mode = 'explicit'
    grammar = (modgrammar.OPTIONAL(modgrammar.WHITESPACE),
               modgrammar.LITERAL("|"),
               modgrammar.OPTIONAL(modgrammar.WHITESPACE))
Example #9
0
class AlternativeWeight(modgrammar.Grammar):
    grammar_whitespace_mode = 'explicit'
    grammar = (modgrammar.L("/"), modgrammar.WORD("0-9.", "0-9.ef"), modgrammar.LITERAL("/"), modgrammar.WHITESPACE)

    def grammar_elem_init(self, session_data):
        self.value = self[1].string
Example #10
0
class Initial(mg.Grammar):
    '''Only capitalized initials are allowed'''
    grammar = (mge.RE(r'\w'), mg.LITERAL('.'))
Example #11
0
class Abrv(mg.Grammar):
    '''unicode possessive, (Was abreviation)'''
    # \u2019 is a right quote mark
    #grammar=(Word, OR(LITERAL('.'), LITERAL('\u2019')))
    grammar = (Word, mg.LITERAL('\u2019'))