Skip to content

Commit

Permalink
Consolidated C23 grammar files
Browse files Browse the repository at this point in the history
Added ##override_root
  • Loading branch information
james-pre committed Jan 5, 2025
1 parent 6a348a4 commit b928c3a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 50 deletions.
14 changes: 0 additions & 14 deletions grammars/c23/common.bnf

This file was deleted.

30 changes: 26 additions & 4 deletions grammars/c23/language.bnf
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -356,7 +378,7 @@ designator_list = designator*;

designator =
| ("\[" constant_expression "\]")
| ("." identifier);
| ("\." identifier);

static_assert_declaration = "static_assert" "\(" constant_expression ["," string_literal] "\)" ";";

Expand Down
28 changes: 12 additions & 16 deletions grammars/c23/preprocessing.bnf
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
##include common.bnf

##root group_part
##include language.bnf

group_part =
| control_line
Expand All @@ -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)
Expand Down Expand Up @@ -159,4 +155,4 @@ pp_number = "\.?[0-9](['\w]|(e|E|p|P)(\+|-)|\.)*";

identifier = "[_a-zA-Z]\w*";

##include punctuator.bnf
##override_root group_part
12 changes: 0 additions & 12 deletions grammars/c23/punctuator.bnf

This file was deleted.

10 changes: 6 additions & 4 deletions src/bnf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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;
Expand Down

0 comments on commit b928c3a

Please sign in to comment.