Fix typedef struct bug

This commit is contained in:
Ganesh Viswanathan 2018-12-27 00:38:29 -06:00
commit c7a3c7b7d6
2 changed files with 35 additions and 20 deletions

View file

@ -100,27 +100,32 @@ proc initGrammar() =
gStateRT.typeStr &= &" {fname}*: {ftyp}\n" gStateRT.typeStr &= &" {fname}*: {ftyp}\n"
i += 2 i += 2
let fieldGrammar = """ let
(field_identifier?) fieldGrammar = """
(array_declarator? (field_identifier?)
(field_identifier) (array_declarator?
(identifier|number_literal) (field_identifier)
) (identifier|number_literal)
""" )
"""
fieldListGrammar = &"""
(field_declaration_list
(field_declaration+
{typeGrammar}
{fieldGrammar}
(pointer_declarator?
{fieldGrammar}
)
)
)
"""
# struct X {} # struct X {}
gStateRT.grammar.add((&""" gStateRT.grammar.add((&"""
(struct_specifier|union_specifier (struct_specifier|union_specifier
(type_identifier) (type_identifier)
(field_declaration_list {fieldListGrammar}
(field_declaration+
{typeGrammar}
{fieldGrammar}
(pointer_declarator?
{fieldGrammar}
)
)
)
) )
""", """,
proc (ast: ref Ast, node: TSNode) = proc (ast: ref Ast, node: TSNode) =
@ -130,7 +135,10 @@ proc initGrammar() =
# typedef struct X {} # typedef struct X {}
gStateRT.grammar.add((&""" gStateRT.grammar.add((&"""
(type_definition (type_definition
{gStateRT.grammar[^1].grammar} (struct_specifier|union_specifier
(type_identifier?)
{fieldListGrammar}
)
(type_identifier) (type_identifier)
) )
""", """,

View file

@ -4,7 +4,7 @@ import treesitter/runtime
import treesitter_c/c import treesitter_c/c
import treesitter_cpp/cpp import treesitter_cpp/cpp
import nimterop/[ast, globals, getters] import nimterop/[ast, globals, getters, grammar]
proc printLisp(root: TSNode) = proc printLisp(root: TSNode) =
var var
@ -112,6 +112,7 @@ proc main(
pnim = false, pnim = false,
pretty = true, pretty = true,
preprocess = false, preprocess = false,
pgrammar = false,
defines: seq[string] = @[], defines: seq[string] = @[],
includeDirs: seq[string] = @[], includeDirs: seq[string] = @[],
source: seq[string], source: seq[string],
@ -126,7 +127,11 @@ proc main(
defines: defines, defines: defines,
includeDirs: includeDirs, includeDirs: includeDirs,
) )
if source.len != 0:
if pgrammar:
parseGrammar()
printGrammar()
elif source.len != 0:
process(source[0]) process(source[0])
when isMainModule: when isMainModule:
@ -138,11 +143,13 @@ when isMainModule:
"defines": "definitions to pass to preprocessor", "defines": "definitions to pass to preprocessor",
"includeDirs": "include directory to pass to preprocessor", "includeDirs": "include directory to pass to preprocessor",
"preprocess": "run preprocessor on header", "preprocess": "run preprocessor on header",
"pgrammar": "print grammar",
"source" : "C/C++ source/header", "source" : "C/C++ source/header",
}, short = { }, short = {
"past": 'a', "past": 'a',
"pnim": 'n', "pnim": 'n',
"defines": 'D', "defines": 'D',
"includeDirs": 'I', "includeDirs": 'I',
"preprocess": 'p' "preprocess": 'p',
"pgrammar": 'g'
}) })