diff --git a/grammars/c23/common.bnf b/grammars/c23/common.bnf deleted file mode 100644 index 0014d31..0000000 --- a/grammars/c23/common.bnf +++ /dev/null @@ -1,14 +0,0 @@ - -comment = "\/\/[^\n]*|\/\*[\s\S]*?\*\/"; -whitespace = '\s+'; - -##ignore comment whitespace - -# String and character constants - -# Due to comments handling, we need to have a literal for the entire character constant -character_constant = "(u8|u|U|L)?'([^'\\\n]|\\['\"\\?abfnrtv]|\\[0-7]{1,3}|\\x[0-9A-Fa-f]+|\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})*'"; - -string_literal = '(u8|u|U|L)?"([^"\\\n]|\\["\'\\?abfnrtv]|\\[0-7]{1,3}|\\x[0-9A-Fa-f]+|\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})*"'; - -quote = "'"; diff --git a/grammars/c23/language.bnf b/grammars/c23/language.bnf index f799e14..7c71a51 100644 --- a/grammars/c23/language.bnf +++ b/grammars/c23/language.bnf @@ -1,4 +1,17 @@ -##include common.bnf + +comment = "\/\/[^\n]*|\/\*[\s\S]*?\*\/"; +whitespace = '\s+'; + +##ignore comment whitespace + +# String and character constants + +# Due to comments handling, we need to have a literal for the entire character constant +character_constant = "(u8|u|U|L)?'([^'\\\n]|\\['\"\\?abfnrtv]|\\[0-7]{1,3}|\\x[0-9A-Fa-f]+|\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})*'"; + +string_literal = '(u8|u|U|L)?"([^"\\\n]|\\["\'\\?abfnrtv]|\\[0-7]{1,3}|\\x[0-9A-Fa-f]+|\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})*"'; + +quote = "'"; token = | keyword @@ -100,7 +113,17 @@ enumeration_constant = identifier; predefined_constant = "false" | "true" | "nullptr"; -##include punctuator.bnf +# `punctuator` minus things used later (e.g. "any pp-token other than ...") +__punctuator_base = "\." | "\+\+" | "--" | "&" | "\*" | "\+" | "-" | "~" | "!" +| "/" | "%" | "==" | "!=" | "\^" | "\|" | "&&" | "\|\|" | "\?" | ":" | "::" | ";" | "\.\.\." +| "=" | "\*=" | "/=" | "%=" | "\+=" | "-=" | "&=" | "\^=" | "\|=" +| "," | "##" | "%:" | "%:%:"; + +__punctuator_left_angle_brackets = "<<" | "<" | "<=" | "<<=" | "<:" | "<%"; +__punctuator_angle_brackets = __punctuator_left_angle_brackets | ">>" | ">" | ">=" | ">>=" | ":>" | "%>" | "->"; +__punctuator_grouping = "\[" | "\]" | "\(" | "\)" | "\{" | "\}"; + +punctuator = __punctuator_base | __punctuator_angle_brackets | __punctuator_grouping; primary_expression = | identifier @@ -113,7 +136,6 @@ generic_selection = "_Generic" "\(" assignment_expression "," generic_assoc_list generic_assoc_list = generic_association {"," generic_association}; - generic_association = (type_name | "default") ":" assignment_expression; postfix_expression = @@ -356,7 +378,7 @@ designator_list = designator*; designator = | ("\[" constant_expression "\]") -| ("." identifier); +| ("\." identifier); static_assert_declaration = "static_assert" "\(" constant_expression ["," string_literal] "\)" ";"; diff --git a/grammars/c23/preprocessing.bnf b/grammars/c23/preprocessing.bnf index 972dabc..0293748 100644 --- a/grammars/c23/preprocessing.bnf +++ b/grammars/c23/preprocessing.bnf @@ -1,6 +1,4 @@ -##include common.bnf - -##root group_part +##include language.bnf group_part = | control_line @@ -10,23 +8,21 @@ group_part = if_section = if_group elif_group* else_group? endif_line; -if_group = "#" ( -| ("if\b" constant_expression group_part*) -| ("ifdef\b" identifier group_part*) -| ("ifndef\b" identifier group_part*) -); +if_group = +| ("#if\b" constant_expression group_part*) +| ("#ifdef\b" identifier group_part*) +| ("#ifndef\b" identifier group_part*); ##groups if_group %#if %#ifdef %#ifndef -elif_group = "#" ( -| ("elif\b" constant_expression group_part*) -| ("elifdef\b" identifier group_part*) -| ("elifndef\b" identifier group_part*) -); +elif_group = +| ("#elif\b" constant_expression group_part*) +| ("#elifdef\b" identifier group_part*) +| ("#elifndef\b" identifier group_part*); ##groups elif_group %#elif %#elifdef %#elifndef -else_group = "#" "else\b" group_part*; +else_group = "#else\b" group_part*; -endif_line = "#" "endif\b"; +endif_line = "#endif\b"; control_line = "#" ( | ("include\b" header_name) @@ -159,4 +155,4 @@ pp_number = "\.?[0-9](['\w]|(e|E|p|P)(\+|-)|\.)*"; identifier = "[_a-zA-Z]\w*"; -##include punctuator.bnf \ No newline at end of file +##override_root group_part diff --git a/grammars/c23/punctuator.bnf b/grammars/c23/punctuator.bnf deleted file mode 100644 index 32482c3..0000000 --- a/grammars/c23/punctuator.bnf +++ /dev/null @@ -1,12 +0,0 @@ - -# `punctuator` minus things used later (e.g. "any pp-token other than ...") -__punctuator_base = "\." | "\+\+" | "--" | "&" | "\*" | "\+" | "-" | "~" | "!" -| "/" | "%" | "==" | "!=" | "\^" | "\|" | "&&" | "\|\|" | "\?" | ":" | "::" | ";" | "\.\.\." -| "=" | "\*=" | "/=" | "%=" | "\+=" | "-=" | "&=" | "\^=" | "\|=" -| "," | "##" | "%:" | "%:%:"; - -__punctuator_left_angle_brackets = "<<" | "<" | "<=" | "<<=" | "<:" | "<%"; -__punctuator_angle_brackets = __punctuator_left_angle_brackets | ">>" | ">" | ">=" | ">>=" | ":>" | "%>" | "->"; -__punctuator_grouping = "\[" | "\]" | "\(" | "\)" | "\{" | "\}"; - -punctuator = __punctuator_base | __punctuator_angle_brackets | __punctuator_grouping; \ No newline at end of file diff --git a/src/bnf.ts b/src/bnf.ts index 4dcd626..f1b30e2 100644 --- a/src/bnf.ts +++ b/src/bnf.ts @@ -35,11 +35,10 @@ const typeForGroup = { export function ast_to_config(ast: Node[], log: Logger = () => {}, include?: (name: string) => Node[]): config.Config { const definitions: PureNodeDefinition[] = [], literals: TokenDefinition[] = [], - ignoreLiterals: string[] = [], - rootNodes: string[] = []; - + ignoreLiterals: string[] = []; let currentNode: string, - groups = 0; + groups = 0, + rootNodes: string[] = []; function processNode(node: Node, depth: number = 0) { const _log = logger(log, { kind: node.kind, depth }); @@ -53,6 +52,9 @@ export function ast_to_config(ast: Node[], log: Logger = () => {}, include?: (na case 'root': rootNodes.push(...contents.split(/[ ,;]/)); break; + case 'override_root': + rootNodes = contents.split(/[ ,;]/); + break; case 'ignore': ignoreLiterals.push(...contents.split(/[ ,;]/)); break;