From c7a3c7b7d606b00fa1c676e244c39002b8fc1609 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Thu, 27 Dec 2018 00:38:29 -0600 Subject: Fix typedef struct bug --- nimterop/grammar.nim | 42 +++++++++++++++++++++++++----------------- toast.nim | 13 ++++++++++--- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index aa364b1..bb789bc 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -100,27 +100,32 @@ proc initGrammar() = gStateRT.typeStr &= &" {fname}*: {ftyp}\n" i += 2 - let fieldGrammar = """ - (field_identifier?) - (array_declarator? - (field_identifier) - (identifier|number_literal) - ) - """ + let + fieldGrammar = """ + (field_identifier?) + (array_declarator? + (field_identifier) + (identifier|number_literal) + ) + """ + + fieldListGrammar = &""" + (field_declaration_list + (field_declaration+ + {typeGrammar} + {fieldGrammar} + (pointer_declarator? + {fieldGrammar} + ) + ) + ) + """ # struct X {} gStateRT.grammar.add((&""" (struct_specifier|union_specifier (type_identifier) - (field_declaration_list - (field_declaration+ - {typeGrammar} - {fieldGrammar} - (pointer_declarator? - {fieldGrammar} - ) - ) - ) + {fieldListGrammar} ) """, proc (ast: ref Ast, node: TSNode) = @@ -130,7 +135,10 @@ proc initGrammar() = # typedef struct X {} gStateRT.grammar.add((&""" (type_definition - {gStateRT.grammar[^1].grammar} + (struct_specifier|union_specifier + (type_identifier?) + {fieldListGrammar} + ) (type_identifier) ) """, diff --git a/toast.nim b/toast.nim index ced3047..f9edd89 100644 --- a/toast.nim +++ b/toast.nim @@ -4,7 +4,7 @@ import treesitter/runtime import treesitter_c/c import treesitter_cpp/cpp -import nimterop/[ast, globals, getters] +import nimterop/[ast, globals, getters, grammar] proc printLisp(root: TSNode) = var @@ -112,6 +112,7 @@ proc main( pnim = false, pretty = true, preprocess = false, + pgrammar = false, defines: seq[string] = @[], includeDirs: seq[string] = @[], source: seq[string], @@ -126,7 +127,11 @@ proc main( defines: defines, includeDirs: includeDirs, ) - if source.len != 0: + + if pgrammar: + parseGrammar() + printGrammar() + elif source.len != 0: process(source[0]) when isMainModule: @@ -138,11 +143,13 @@ when isMainModule: "defines": "definitions to pass to preprocessor", "includeDirs": "include directory to pass to preprocessor", "preprocess": "run preprocessor on header", + "pgrammar": "print grammar", "source" : "C/C++ source/header", }, short = { "past": 'a', "pnim": 'n', "defines": 'D', "includeDirs": 'I', - "preprocess": 'p' + "preprocess": 'p', + "pgrammar": 'g' }) -- cgit v1.2.3