From 1e196967e429c2ff22b3b5af4500f4902ca90ae6 Mon Sep 17 00:00:00 2001 From: Markus Pfeiffer Date: Mon, 9 Jul 2018 04:57:05 +0100 Subject: [PATCH 1/3] Refactor compound_statement --- ward/cparser.lua | 87 +++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/ward/cparser.lua b/ward/cparser.lua index e289d73..c7d81bc 100644 --- a/ward/cparser.lua +++ b/ward/cparser.lua @@ -546,6 +546,38 @@ local function build_for_stmt(init, cond, step, body) return init_node, exit_node end +local function build_compount_stmt_local_variables(str, pos, declarations) + local local_declarations = { } + for _, declaration in ipairs(declarations) do + local name, type, vpos, value = unpack(declaration) + local storage = declaration[5] + if type:is_function() then + local funcdef = storage + funcdef.type = type.result_type + funcdef.arg_types = type.arg_types + funcdef.name = name + funcdef.filename = source_file_name + funcdef.graph = nil + if storage.static then + if not static_functions[name] then + static_functions[name] = funcdef + end + else + if not global_functions[name] then + global_functions[name] = funcdef + end + end + else + if storage.extern then + global_variables[name] = type + else + push(local_declarations, declaration); + end + end + end + return pos, local_declarations +end + local function build_compound_stmt_part(decls, stmts) local first, last local head_node = make_node() @@ -1060,47 +1092,20 @@ local grammar = pattern { function (str, pos, start, body, cond, finish) return pos, { build_do_stmt, start, finish, body, cond } end), - compound_statement = action(action(pattern "{", function(str, pos) - return pos, pos-1 - end) * sp * - aggregate( - aggregate((rule "statement" * sp) ^ 0) * - (aggregate(action(rule "variable_declaration" * sp, - function(str, pos, declarations) - local local_declarations = { } - for _, declaration in ipairs(declarations) do - local name, type, vpos, value = unpack(declaration) - local storage = declaration[5] - if type:is_function() then - local funcdef = storage - funcdef.type = type.result_type - funcdef.arg_types = type.arg_types - funcdef.name = name - funcdef.filename = source_file_name - funcdef.graph = nil - if storage.static then - if not static_functions[name] then - static_functions[name] = funcdef - end - else - if not global_functions[name] then - global_functions[name] = funcdef - end - end - else - if storage.extern then - global_variables[name] = type - else - push(local_declarations, declaration); - end - end - end - return pos, local_declarations - end) ^ 1) * - aggregate((rule "statement" * sp) ^ 0)) ^ 0 - ) * pattern "}" * position(), - function(str, pos, start, decls_and_stmts, finish) - return pos, { build_compound_stmt, start, finish, decls_and_stmts } + compound_statement = + action( + action(pattern "{", function(str, pos) + return pos, pos-1 + end) * sp * + aggregate( + aggregate((rule "statement" * sp) ^ 0) * + (aggregate(action(rule "variable_declaration" * sp, + build_compount_stmt_local_variables) ^ 1) * + aggregate((rule "statement" * sp) ^ 0)) ^ 0 + ) * + pattern "}" * position(), + function(str, pos, start, decls_and_stmts, finish) + return pos, { build_compound_stmt, start, finish, decls_and_stmts } end), -- Warning: The following hack allows ward to parse C99 'for' loops which declare -- variables in their init section, like in this snippet: From f0c8b7661b02a85b4ab68e1c4ec1923184fbb689 Mon Sep 17 00:00:00 2001 From: Markus Pfeiffer Date: Mon, 9 Jul 2018 05:08:14 +0100 Subject: [PATCH 2/3] Aggregate expressions can be empty --- ward/cparser.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ward/cparser.lua b/ward/cparser.lua index c7d81bc..5fa3820 100644 --- a/ward/cparser.lua +++ b/ward/cparser.lua @@ -1025,9 +1025,11 @@ local grammar = pattern { rule "aggregate_expression"+ rule "vararg_expr", aggregate_expression = - action(pattern "{" * sp * ("." * sp)^-1 *rule "expression" * - (sp * pattern "," * sp * ("." * sp)^-1 *rule "expression")^0 * - (pattern ",")^-1 * sp * pattern "}", function(str, pos) + action(pattern "{" * + (sp * ("." * sp)^-1 *rule "expression" * + (sp * pattern "," * sp * ("." * sp)^-1 *rule "expression")^0 * + (pattern ",")^-1) ^ 0 * + sp * pattern "}", function(str, pos) return pos, new(ExprConstant) end), stmt_expression = From 1e8dbd82214bf1c055aa39fff3a9e3a4b7663c8a Mon Sep 17 00:00:00 2001 From: Markus Pfeiffer Date: Mon, 9 Jul 2018 05:28:08 +0100 Subject: [PATCH 3/3] Support C99 positional array initialisation --- ward/cparser.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ward/cparser.lua b/ward/cparser.lua index 5fa3820..5fa54df 100644 --- a/ward/cparser.lua +++ b/ward/cparser.lua @@ -546,7 +546,7 @@ local function build_for_stmt(init, cond, step, body) return init_node, exit_node end -local function build_compount_stmt_local_variables(str, pos, declarations) +local function build_compound_stmt_local_variables(str, pos, declarations) local local_declarations = { } for _, declaration in ipairs(declarations) do local name, type, vpos, value = unpack(declaration) @@ -886,8 +886,10 @@ local grammar = pattern { opt_asm_declaration = (sp * (keyword "asm" + keyword "__asm__" + keyword "__asm") * sp * pattern "(" * sp * string_constant * sp * pattern ")")^-1, - init_expression_list = (rule "init_expression" * (sp * pattern "," * sp * - rule "init_expression")^0 * (sp * pattern ",")^-1)^-1, + init_expression_list_part = + (pattern "[" * sp * expression * sp * pattern "]" * sp * pattern "=" * sp) ^ 0 * rule "init_expression", + init_expression_list = (rule "init_expression_list_part" * (sp * pattern "," * sp * + rule "init_expression_list_part")^0 * (sp * pattern ",")^-1)^-1, init_expression = action(pattern "{" * position() * sp * rule "init_expression_list" * sp * pattern "}", function(str, pos, init_pos) @@ -1102,7 +1104,7 @@ local grammar = pattern { aggregate( aggregate((rule "statement" * sp) ^ 0) * (aggregate(action(rule "variable_declaration" * sp, - build_compount_stmt_local_variables) ^ 1) * + build_compound_stmt_local_variables) ^ 1) * aggregate((rule "statement" * sp) ^ 0)) ^ 0 ) * pattern "}" * position(),