Created
November 11, 2023 12:13
-
-
Save jerry73204/1105dd6962de1b7c3d89e0ae3d2d6671 to your computer and use it in GitHub Desktop.
Pest Grammar for Common Trace Format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // lexical elements | |
| token = { | |
| keyword | |
| | identifier | |
| | constant | |
| | string_literal | |
| | punctuator | |
| } | |
| // keywords | |
| keyword = { | |
| "align" | |
| | "callsite" | |
| | "const" | |
| | "char" | |
| | "clock" | |
| | "double" | |
| | "enum" | |
| | "env" | |
| | "event" | |
| | "floating_point" | |
| | "float" | |
| | "integer" | |
| | "int" | |
| | "long" | |
| | "short" | |
| | "signed" | |
| | "stream" | |
| | "string" | |
| | "struct" | |
| | "trace" | |
| | "typealias" | |
| | "typedef" | |
| | "unsigned" | |
| | "variant" | |
| | "void" | |
| | "_Bool" | |
| | "_Complex" | |
| | "_Imaginary" | |
| } | |
| // identifiers | |
| identifier = @{ | |
| identifier_nondigit ~ ( identifier_nondigit | digit )* | |
| } | |
| identifier_nondigit = { | |
| nondigit | |
| | universal_character_name | |
| } | |
| nondigit = { 'a'..'z' | 'A'..'Z' } | |
| digit = { '0'..'9' } | |
| // universal character names | |
| universal_character_name = @{ | |
| ("\\u" ~ hex_quad) | |
| | ("\\U" ~ hex_quad ~ hex_quad) | |
| } | |
| hex_quad = @{ hexadecimal_digit{4} } | |
| //# constants | |
| constant = { | |
| integer_constant | |
| | enumeration_constant | |
| | character_constant | |
| } | |
| integer_constant = @{ | |
| ( decimal_constant ~ integer_suffix? ) | |
| | ( octal_constant ~ integer_suffix? ) | |
| | ( hexadecimal_constant ~ integer_suffix? ) | |
| } | |
| decimal_constant = @{ | |
| nonzero_digit ~ digit* | |
| } | |
| octal_constant = @{ | |
| "0" ~ octal_digit* | |
| } | |
| hexadecimal_constant = @{ | |
| hexadecimal_prefix ~ hexadecimal_digit+ | |
| } | |
| hexadecimal_prefix = { | |
| "0x" | |
| | "0X" | |
| } | |
| nonzero_digit = { '1'..'9' } | |
| hexadecimal_digit = { '0'..'9' | 'a'..'f' } | |
| octal_digit = { '0'..'7' } | |
| integer_suffix = { | |
| ( unsigned_suffix ~ long_suffix? ) | |
| | ( unsigned_suffix ~ long_long_suffix ) | |
| | ( long_suffix ~ unsigned_suffix? ) | |
| | ( long_long_suffix ~ unsigned_suffix? ) | |
| } | |
| unsigned_suffix = { "u" | "U" } | |
| long_suffix = { "l" | "L" } | |
| long_long_suffix = { "ll" | "LL" } | |
| enumeration_constant = { | |
| identifier | |
| | string_literal | |
| } | |
| character_constant = @{ | |
| ( "'" ~ c_char_sequence ~ "'" ) | |
| | ( "L'" ~ c_char_sequence ~ "'" ) | |
| } | |
| c_char_sequence = @{ c_char+ } | |
| c_char = { | |
| ( ! ( "'" | "\\") ~ ANY ) | |
| | escape_sequence | |
| } | |
| escape_sequence = { | |
| simple_escape_sequence | |
| | octal_escape_sequence | |
| | hexadecimal_escape_sequence | |
| | universal_character_name | |
| } | |
| simple_escape_sequence = @{ | |
| "\\" ~ ( "'" | "\"" | "?" | "\\" | "a" | "b" | "f" | "n" | "r" | "t" | "v" ) | |
| } | |
| octal_escape_sequence = @{ | |
| "\\" ~ octal_digit{1,3} | |
| } | |
| hexadecimal_escape_sequence = @{ | |
| "\\x" ~ hexadecimal_digit+ | |
| } | |
| // string literals | |
| string_literal = @{ | |
| ( ("\"" | "L\"" ) ~ s_char_sequence? ~ "\"" ) | |
| } | |
| s_char_sequence = @{ | |
| s_char+ | |
| } | |
| s_char = { | |
| ( ! ( "\"" | "\\" ) ~ ANY ) | |
| | escape_sequence | |
| } | |
| // punctuators | |
| punctuator = { | |
| "[" | |
| | "]" | |
| | "(" | |
| | ")" | |
| | "{" | |
| | "}" | |
| | "." | |
| | "->" | |
| | "*" | |
| | "+" | |
| | "-" | |
| | "<" | |
| | ">" | |
| | ":" | |
| | ";" | |
| | "." | |
| |"." | |
| |"." | |
| | "=" | |
| | "," | |
| } | |
| // Phrase structure grammar | |
| primary_expression = { | |
| identifier | |
| | constant | |
| | string_literal | |
| | ( "(" ~ unary_expression ~ ")" ) | |
| } | |
| postfix_expression = { | |
| primary_expression | |
| ~ | |
| ( | |
| ( "[" ~ unary_expression ~ "]" ) | |
| | ( "." ~ identifier ) | |
| | ( "->" ~ identifier ) | |
| )* | |
| } | |
| unary_expression = { | |
| unary_operator? ~ postfix_expression | |
| } | |
| unary_operator = { "+" | "-" } | |
| assignment_operator = { "=" } | |
| type_assignment_operator = { ":=" } | |
| constant_expression_range = @{ | |
| unary_expression ~ "..." ~ unary_expression | |
| } | |
| // declarations | |
| declaration = { | |
| ( | |
| ( declaration_specifiers ~ declarator_list? ) | ( ctf_specifier ) | |
| ) | |
| ~ | |
| ";" | |
| } | |
| declaration_specifiers = { | |
| ( storage_class_specifier ~ declaration_specifiers? ) | |
| | ( type_specifier ~ declaration_specifiers? ) | |
| | ( type_qualifier ~ declaration_specifiers? ) | |
| } | |
| declarator_list = { | |
| declarator ~ ( "," ~ declarator )* | |
| } | |
| abstract_declarator_list = { | |
| abstract_declarator ~ ( "," ~ abstract_declarator )* | |
| } | |
| storage_class_specifier = { "typedef" } | |
| type_specifier = { | |
| "void" | |
| | "char" | |
| | "short" | |
| | "int" | |
| | "long" | |
| | "float" | |
| | "double" | |
| | "signed" | |
| | "unsigned" | |
| | "_Bool" | |
| | "_Complex" | |
| | "_Imaginary" | |
| | struct_specifier | |
| | variant_specifier | |
| | enum_specifier | |
| | typedef_name | |
| | ctf_type_specifier | |
| } | |
| align_attribute = { | |
| "align" ~ "(" ~ unary_expression ~ ")" | |
| } | |
| struct_specifier = { | |
| ( "struct" ~ identifier? ~ "{" ~ struct_or_variant_declaration_list? ~ "}" ~ align_attribute? ) | |
| | ( "struct" ~ identifier ~ align_attribute? ) | |
| } | |
| struct_or_variant_declaration_list = { | |
| struct_or_variant_declaration+ | |
| } | |
| struct_or_variant_declaration = { | |
| ( specifier_qualifier_list ~ struct_or_variant_declarator_list ~ ";" ) | |
| | ( declaration_specifiers? ~ storage_class_specifier ~ declaration_specifiers? ~ declarator_list ~ ";" ) | |
| | ( "typealias" ~ declaration_specifiers ~ abstract_declarator_list ~ type_assignment_operator ~ declaration_specifiers ~ abstract_declarator_list ~ ";" ) | |
| | ( "typealias" ~ declaration_specifiers ~ abstract_declarator_list ~ type_assignment_operator ~ declarator_list ~ ";" ) | |
| } | |
| specifier_qualifier_list = { | |
| ( type_specifier ~ specifier_qualifier_list? ) | |
| | ( type_qualifier ~ specifier_qualifier_list? ) | |
| } | |
| struct_or_variant_declarator_list = { | |
| struct_or_variant_declarator ~ ( "," ~ struct_or_variant_declarator )* | |
| } | |
| struct_or_variant_declarator = { | |
| declarator? ~ ( ":" ~ unary_expression )* | |
| } | |
| variant_specifier = { | |
| ( "variant" ~ identifier? ~ variant_tag? ~ "{" ~ struct_or_variant_declaration_list ~ "}" ) | |
| | ( "variant" ~ identifier ~ variant_tag ) | |
| } | |
| variant_tag = { | |
| "<" ~ unary_expression ~ ">" | |
| } | |
| enum_specifier = { | |
| ( "enum" ~ identifier? ~ "{" ~ enumerator_list ~ "}" ) | |
| | ( "enum" ~ identifier? ~ "{" ~ enumerator_list ~ "," ~ "}" ) | |
| | ( "enum" ~ identifier ) | |
| | ( "enum" ~ identifier? ~ ":" ~ declaration_specifiers ~ "{" ~ enumerator_list ~ "}" ) | |
| | ( "enum" ~ identifier? ~ ":" ~ declaration_specifiers ~ "{" ~ enumerator_list ~ "," ~ "}" ) | |
| } | |
| enumerator_list = { | |
| enumerator ~ ( "," ~ enumerator )* | |
| } | |
| enumerator = { | |
| enumeration_constant | |
| | ( enumeration_constant ~ assignment_operator ~ unary_expression ) | |
| | ( enumeration_constant ~ assignment_operator ~ constant_expression_range ) | |
| } | |
| type_qualifier = { "const" } | |
| declarator = { | |
| ( pointer? ~ direct_declarator ) | |
| } | |
| direct_declarator = { | |
| ( identifier | ( "(" ~ declarator ~ ")" ) ) | |
| ~ | |
| ( "[" ~ unary_expression ~ "]" )* | |
| } | |
| abstract_declarator = _{ | |
| pointer? ~ direct_abstract_declarator | |
| } | |
| direct_abstract_declarator = { | |
| ( | |
| identifier // BUG: It was identifier-opt but cannot compile. | |
| | ( "(" ~ abstract_declarator ~ ")" ) | |
| ) | |
| ~ | |
| ( | |
| ( "[" ~ unary_expression ~ "]" ) | |
| | ( "[" ~ "]" ) | |
| )* | |
| } | |
| pointer = { | |
| ( "*" ~ type_qualifier_list? ) | |
| | ( "*" ~ type_qualifier_list? ~ pointer ) | |
| } | |
| type_qualifier_list = { | |
| type_qualifier+ | |
| } | |
| typedef_name = { identifier } | |
| // CTF-specific declarations | |
| ctf_specifier = { | |
| ( "clock" ~ "{" ~ ctf_assignment_expression_list? ~ "}" ) | |
| | ( "event" ~ "{" ~ ctf_assignment_expression_list? ~ "}" ) | |
| | ( "stream" ~ "{" ~ ctf_assignment_expression_list? ~ "}" ) | |
| | ( "env" ~ "{" ~ ctf_assignment_expression_list? ~ "}" ) | |
| | ( "trace" ~ "{" ~ ctf_assignment_expression_list? ~ "}" ) | |
| | ( "callsite" ~ "{" ~ ctf_assignment_expression_list? ~ "}" ) | |
| | ( "typealias" ~ declaration_specifiers ~ abstract_declarator_list ~ type_assignment_operator ~ declaration_specifiers ~ abstract_declarator_list ) | |
| | ( "typealias" ~ declaration_specifiers ~ abstract_declarator_list ~ type_assignment_operator ~ declarator_list ) | |
| } | |
| ctf_type_specifier = { | |
| ( "floating_point" ~ "{" ~ ctf_assignment_expression_list? ~ "}" ) | |
| | ( "integer" ~ "{" ~ ctf_assignment_expression_list? ~ "}" ) | |
| | ( "string" ~ "{" ~ ctf_assignment_expression_list? ~ "}" ) | |
| | "string" | |
| } | |
| ctf_assignment_expression_list = { | |
| ctf_assignment_expression+ ~ ";" | |
| } | |
| ctf_assignment_expression = { | |
| ( unary_expression ~ assignment_operator ~ unary_expression ) | |
| | ( unary_expression ~ type_assignment_operator ~ type_specifier ) | |
| | ( declaration_specifiers? ~ storage_class_specifier ~ declaration_specifiers? ~ declarator_list ) | |
| | ( "typealias" ~ declaration_specifiers ~ abstract_declarator_list ~ type_assignment_operator ~ declaration_specifiers ~ abstract_declarator_list ) | |
| | ( "typealias" ~ declaration_specifiers ~ abstract_declarator_list ~ type_assignment_operator ~ declarator_list ) | |
| } | |
| // file | |
| file = _{ declaration* } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment