How to use the pyparsing.Literal function in pyparsing

To help you get started, we’ve selected a few pyparsing examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kjellmf / dot2tex / dot2tex / dotparsing.py View on Github external
def define_dot_parser(self):
        """Define dot grammar

        Based on the grammar http://www.graphviz.org/doc/info/lang.html
        """
        # punctuation
        colon = Literal(":")
        lbrace = Suppress("{")
        rbrace = Suppress("}")
        lbrack = Suppress("[")
        rbrack = Suppress("]")
        lparen = Literal("(")
        rparen = Literal(")")
        equals = Suppress("=")
        comma = Literal(",")
        dot = Literal(".")
        slash = Literal("/")
        bslash = Literal("\\")
        star = Literal("*")
        semi = Suppress(";")
        at = Literal("@")
        minus = Literal("-")
        pluss = Suppress("+")

        # keywords
        strict_ = CaselessLiteral("strict")
        graph_ = CaselessLiteral("graph")
        digraph_ = CaselessLiteral("digraph")
        subgraph_ = CaselessLiteral("subgraph")
        node_ = CaselessLiteral("node")
        edge_ = CaselessLiteral("edge")
github certbot / certbot / certbot-nginx / certbot_nginx / _internal / nginxparser.py View on Github external
import six

logger = logging.getLogger(__name__)

class RawNginxParser(object):
    # pylint: disable=expression-not-assigned
    # pylint: disable=pointless-statement
    """A class that parses nginx configuration with pyparsing."""

    # constants
    space = Optional(White()).leaveWhitespace()
    required_space = White().leaveWhitespace()

    left_bracket = Literal("{").suppress()
    right_bracket = space + Literal("}").suppress()
    semicolon = Literal(";").suppress()
    dquoted = QuotedString('"', multiline=True, unquoteResults=False, escChar='\\')
    squoted = QuotedString("'", multiline=True, unquoteResults=False, escChar='\\')
    quoted = dquoted | squoted
    head_tokenchars = Regex(r"(\$\{)|[^{};\s'\"]") # if (last_space)
    tail_tokenchars = Regex(r"(\$\{)|[^{;\s]") # else
    tokenchars = Combine(head_tokenchars + ZeroOrMore(tail_tokenchars))
    paren_quote_extend = Combine(quoted + Literal(')') + ZeroOrMore(tail_tokenchars))
    # note: ')' allows extension, but then we fall into else, not last_space.

    token = paren_quote_extend | tokenchars | quoted

    whitespace_token_group = space + token + ZeroOrMore(required_space + token) + space
    assignment = whitespace_token_group + semicolon

    comment = space + Literal('#') + restOfLine
github Arelle / Arelle / arelle / plugin / formulaLoader.py View on Github external
separator = Suppress( Literal(";") )
    
    namespaceDeclaration = (Suppress(Keyword("namespace")) + ncName + Suppress(Literal("=")) + quotedString + separator
                            ).setParseAction(compileNamespaceDeclaration).ignore(xfsComment)
    defaultDeclaration = (Suppress(Keyword("unsatisfied-severity") | Keyword("default-language")) + ncName + separator
                         ).setParseAction(compileDefaults).ignore(xfsComment)

    parameterDeclaration = (Suppress(Keyword("parameter")) + qName  +  
                            Suppress(Literal("{")) +
                            Optional(Keyword("required")) +
                            Optional(Keyword("select") + xpathExpression) +
                            Optional(Keyword("as") + qName) + 
                            Suppress(Literal("}")) + separator
                           ).setParseAction(compileParameterDeclaration).ignore(xfsComment)
                           
    occurenceIndicator = Literal("?") | Literal("*") | Literal("+")
                           
    functionParameter = (qName + Suppress(Keyword("as")) + Combine(qName + Optional(occurenceIndicator))
                         ).setParseAction(compileFunctionParameter).ignore(xfsComment)
                         
    functionStep = (Suppress(Keyword("step")) + variableRef + xpathExpression + 
                    separator).setParseAction(compileFunctionStep).ignore(xfsComment)
                         
    functionImplementation = (Suppress(Literal("{")) + 
                              ZeroOrMore(functionStep) + 
                              Keyword("return") + xpathExpression + separator +
                              Suppress(Literal("}"))).ignore(xfsComment)
    
    functionDeclaration = (Suppress(Keyword("function")) + qName  + 
                           Suppress(Literal("(")) + Optional(delimitedList(functionParameter)) + Suppress(Literal(")")) +
                              Keyword("as") + Combine(qName + Optional(occurenceIndicator)) +
                           Optional(functionImplementation) + separator
github roman-kutlak / nlglib / nlglib / logic / fol.py View on Github external
__repr__ = __str__

    def to_expr(self):
        return Expr(self.op, *self.args)


# code nicked from the book Programming in Python 3 (kindle)
# optimisation -- before creating any parsing elements
ParserElement.enablePackrat()

# allow python style comments
comment = (Literal('#') + restOfLine).suppress()

LP, RP, colon = map(Suppress, '():')
forall = Keyword('forall') | Literal('\u2200')
exists = Keyword('exists') | Literal('\u2203')
implies = Keyword('==>') | Keyword('implies') | Literal('\u2192') | Literal('->')
implied = Keyword('<==') | Keyword('impliedby') | Literal('\u2190') | Literal('<-')
iff = Keyword('<=>') | Keyword('iff') | Literal('\u2194') | Keyword('<->')
or_ = Keyword('\\/') | Literal('|') | Keyword('or') | Literal('\u2228')
and_ = Keyword('/\\') | Literal('&') | Keyword('and') | Literal('\u2227')
not_ = Literal('~') | Keyword('not') | Literal('\u00AC')
equals = Literal('=') | Keyword('equals')
notequals = Literal('=/=') | Literal('!=') | \
            Keyword('notequals') | Literal('\u2260')
boolean = (CaselessKeyword('FALSE') | CaselessKeyword('TRUE'))

variable = (~(and_ | or_ | not_ | forall | exists | implied | implies | iff) +
            Combine(Optional('?') + Word(alphanums + "'_£")))
constant = (~(and_ | or_ | not_ | forall | exists | implied | implies | iff) +
            Word(alphas, alphanums + "'-_"))
number = Combine(Optional(oneOf('+ -')) + Word(nums) +
github sfepy / sfepy / genDocs.py View on Github external
def create_bnf( slist, current_section ):
    
    colon = pp.Literal( ':' )

    section = pp.Combine( colon
                          + pp.Word( pp.alphas, pp.alphanums + '_ ' )
                          + colon )
    section.set_parse_action( set_section( current_section ) )
    section.set_name( 'section' )
#    section.set_debug()

    text = pp.SkipTo( section | pp.StringEnd() )
    text.set_parse_action( to_list( slist, current_section ) )
    text.set_name( 'text' )
#    text.set_debug()

##     doc = pp.StringStart()\
##           + pp.ZeroOrMore( section + text )\
##           + pp.StringEnd()
github iotile / coretools / iotilesensorgraph / iotile / sg / node_descriptor.py View on Github external
number = Regex('((0x[a-fA-F0-9]+)|[0-9]+)')
combiner = (Literal('&&') | Literal('||'))
symbol = Regex('[a-zA-Z][a-zA-Z_]*')

stream_type = Optional(Literal('system')) + (Literal('input') | Literal('output') | Literal('buffered') | Literal("unbuffered") | Literal("constant") | Literal("counter")) + Optional(Literal("node").suppress())
stream = stream_type + number

trigger_type = (Literal('value') | Literal('count'))
trigger_op = oneOf('> < >= <= ==')

trigger = Literal('always') | (Literal('when').suppress() + trigger_type('type') + trigger_op('op') + number('reference'))

inputstring = stream('input_stream') + trigger

inputdesc2 = Literal('(').suppress() + inputstring('input_a') + combiner('combiner') + inputstring('input_b') + Literal(')').suppress()
inputdesc1 = Literal('(').suppress() + inputstring('input_a') + Literal(')').suppress()

inputdesc = inputdesc1('input1') | inputdesc2('input2')
graph_node = inputdesc + Literal('=>').suppress() + stream('node') + Literal('using').suppress() + symbol('processor')


def parse_node_descriptor(desc, model):
    """Parse a string node descriptor.

    The function creates an SGNode object without connecting its inputs and outputs
    and returns a 3-tuple:

    SGNode, [(input X, trigger X)], 

    Args:
        desc (str): A description of the node to be created.
github systemd / systemd / hwdb / parse_hwdb.py View on Github external
('ID_INPUT_TOUCHSCREEN', Literal('1')),
             ('ID_INPUT_TRACKBALL', Literal('1')),
             ('MOUSE_WHEEL_TILT_HORIZONTAL', Literal('1')),
             ('MOUSE_WHEEL_TILT_VERTICAL', Literal('1')),
             ('POINTINGSTICK_SENSITIVITY', INTEGER),
             ('POINTINGSTICK_CONST_ACCEL', REAL),
             ('ID_INPUT_JOYSTICK_INTEGRATION', Or(('internal', 'external'))),
             ('ID_INPUT_TOUCHPAD_INTEGRATION', Or(('internal', 'external'))),
             ('XKB_FIXED_LAYOUT', STRING),
             ('XKB_FIXED_VARIANT', STRING),
             ('KEYBOARD_LED_NUMLOCK', Literal('0')),
             ('KEYBOARD_LED_CAPSLOCK', Literal('0')),
             ('ACCEL_MOUNT_MATRIX', mount_matrix),
             ('ACCEL_LOCATION', Or(('display', 'base'))),
            )
    fixed_props = [Literal(name)('NAME') - Suppress('=') - val('VALUE')
                   for name, val in props]
    kbd_props = [Regex(r'KEYBOARD_KEY_[0-9a-f]+')('NAME')
                 - Suppress('=') -
                 ('!' ^ (Optional('!') - Word(alphanums + '_')))('VALUE')
                ]
    abs_props = [Regex(r'EVDEV_ABS_[0-9a-f]{2}')('NAME')
                 - Suppress('=') -
                 Word(nums + ':')('VALUE')
                ]

    grammar = Or(fixed_props + kbd_props + abs_props) + EOL

    return grammar
github RPCS3 / discord-bot / math_parse.py View on Github external
addop   :: '+' | '-'
        integer :: ['+' | '-'] '0'..'9'+
        atom    :: PI | E | real | fn '(' expr ')' | '(' expr ')'
        factor  :: atom [ expop factor ]*
        term    :: factor [ multop factor ]*
        expr    :: term [ addop term ]*
        """
        point = Literal(".")
        e = CaselessLiteral("E")
        fnumber = Combine(Word("+-" + nums, nums) +
                          Optional(point + Optional(Word(nums))) +
                          Optional(e + Word("+-" + nums, nums)))
        ident = Word(alphas, alphas + nums + "_$")
        plus = Literal("+")
        minus = Literal("-")
        mult = Literal("*")
        div = Literal("/")
        lpar = Literal("(").suppress()
        rpar = Literal(")").suppress()
        addop = plus | minus
        multop = mult | div
        expop = Literal("^")
        pi = CaselessLiteral("PI")
        expr = Forward()
        atom = ((Optional(oneOf("- +")) +
                 (ident + lpar + expr + rpar | pi | e | fnumber).setParseAction(self.pushFirst))
                | Optional(oneOf("- +")) + Group(lpar + expr + rpar)
                ).setParseAction(self.pushUMinus)
        # by defining exponentiation as "atom [ ^ factor ]..." instead of
        # "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-right
        # that is, 2^3^2 = 2^(3^2), not (2^3)^2.
        factor = Forward()
github programa-stic / barf-project / barf / arch / x86 / parser.py View on Github external
segment = Or([
    Literal("cs"),
    Literal("ds"),
    Literal("ss"),
    Literal("es"),
    Literal("fs"),
    Literal("gs"),
])("segment")

register = Or([
    segment,
    Word(alphas),
    Combine(Literal("r") + Word(alphanums)),
    Combine(Literal("st") + Word(nums)),
    Combine(Literal("st") + Suppress(Literal("(")) + Word(nums) + Suppress(Literal(")"))),
    Combine(Literal("xmm") + Word(nums)),
    Combine(Literal("ymm") + Word(nums)),
    Combine(Literal("mm") + Word(nums)),
    Combine(Literal("dr") + Word(nums)),
    Combine(Literal("cr") + Word(nums)),
])

base = register("base")

scale = Or([
    Literal("1"),
    Literal("2"),
    Literal("4"),
    Literal("8"),
    Literal("0x1"),
    Literal("0x2"),
github pyparsing / pyparsing / src / unitTests.py View on Github external
res = g1.parseString(teststring)
            print_(res.get("A","A not found")[0])
            print_(res.get("D","!D"))
            assert res.get("A","A not found")[0] == "a", "get on existing key failed"
            assert res.get("D","!D") == "!D", "get on missing key failed"
        
        if "I" in runtests:
            print_("verify handling of Optional's beyond the end of string")
            testGrammar = "A" + pyparsing.Optional("B") + pyparsing.Optional("C") + pyparsing.Optional("D")
            testGrammar.parseString("A")
            testGrammar.parseString("AB")
        
        # test creating Literal with empty string
        if "J" in runtests:
            print_('verify non-fatal usage of Literal("")')
            e = pyparsing.Literal("")
            try:
                e.parseString("SLJFD")
            except Exception as e:
                assert False, "Failed to handle empty Literal"
                
        # test line() behavior when starting at 0 and the opening line is an \n
        if "K" in runtests:
            print_('verify correct line() behavior when first line is empty string')
            assert pyparsing.line(0, "\nabc\ndef\n") == '', "Error in line() with empty first line in text"
            txt = "\nabc\ndef\n"
            results = [ pyparsing.line(i,txt) for i in range(len(txt)) ]
            assert results == ['', 'abc', 'abc', 'abc', 'abc', 'def', 'def', 'def', 'def'], "Error in line() with empty first line in text"
            txt = "abc\ndef\n"
            results = [ pyparsing.line(i,txt) for i in range(len(txt)) ]
            assert results == ['abc', 'abc', 'abc', 'abc', 'def', 'def', 'def', 'def'], "Error in line() with non-empty first line in text"