| Home | Libraries | People | FAQ | More

## Python Syntax Highlighting Grammar

    program
        =
        *(  macro
        |   comment
        |   keyword
        |   identifier
        |   special
        |   string_
        |   number
        |   space_p
        |   anychar_p
        )
        ;

    acro
       =   *space_p >> self.macro
       ;

    comment
        =   +(*space_p >> comment_p("#"))
        ;

    keyword
        =   *space_p >> keyword_ >> (eps_p - (alnum_p | '_'))
        ;   // make sure we recognize whole words only

    keyword_
        =
        "and",       "del",       "for",       "is",        "raise",
        "assert",    "elif",      "from",      "lambda",    "return",
        "break",     "else",      "global",    "not",       "try",
        "class",     "except",    "if",        "or",        "while",
        "continue",  "exec",      "import",    "pass",      "yield",
        "def",       "finally",   "in",        "print",

        // Technically "as" and "None" are not yet keywords (at Python
        // 2.4). They are destined to become keywords, and we treat them
        // as such for syntax highlighting purposes.

        "as", "None"
        ;

    special
        =   *space_p >> +chset_p("~!%^&*()+={[}]:;,<.>/|\\-")
        ;

    string_prefix
        =    as_lower_d[str_p("u") >> ! str_p("r")]
        ;

    string_
        =   *space_p >> ! string_prefix >> (long_string | short_string)
        ;

    short_string
        =   confix_p('"', * c_escape_ch_p, '"') |
            confix_p('\'', * c_escape_ch_p, '\'')
        ;

    long_string
        =   confix_p("'''", * lex_escape_ch_p, "'''") |
            confix_p("\"\"\"", * lex_escape_ch_p, "\"\"\"")
        ;

    number
        =   *space_p >>
            (
                as_lower_d["0x"] >> hex_p
            |   '0' >> oct_p
            |   real_p
            )
            >>  *as_lower_d[chset_p("lj")]
        ;

    identifier
        =   *space_p >> ((alpha_p | '_') >> *(alnum_p | '_'))
        ;

| Copyright © 2002, 2004 Joel de Guzman, Eric Niebler