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"
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)
)
""",

View file

@ -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'
})