| Home | Libraries | People | FAQ | More

## C++ Syntax Highlighting Grammar

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

    macro
        =   *space_p >> self.macro
        ;

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

    comment
        =   +(*space_p >> (comment_p("//") | comment_p("/*", "*/")))
        ;

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

    keyword_
        =   "and_eq", "and", "asm", "auto", "bitand", "bitor",
            "bool", "break", "case", "catch", "char", "class",
            "compl", "const_cast", "const", "continue", "default",
            "delete", "do", "double", "dynamic_cast",  "else",
            "enum", "explicit", "export", "extern", "false",
            "float", "for", "friend", "goto", "if", "inline",
            "int", "long", "mutable", "namespace", "new", "not_eq",
            "not", "operator", "or_eq", "or", "private",
            "protected", "public", "register", "reinterpret_cast",
            "return", "short", "signed", "sizeof", "static",
            "static_cast", "struct", "switch", "template", "this",
            "throw", "true", "try", "typedef", "typeid",
            "typename", "union", "unsigned", "using", "virtual",
            "void", "volatile", "wchar_t", "while", "xor_eq", "xor"
        ;

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

    string_
        =   *space_p >> !as_lower_d['l'] >> confix_p('"', *c_escape_ch_p, '"')
        ;

    char_
        =   *space_p >> !as_lower_d['l'] >> confix_p('\'', *c_escape_ch_p, '\'')
        ;

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

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

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