Cleanup abstract pointers, typedef function support
This commit is contained in:
parent
a1d920bc6a
commit
8c644e814c
2 changed files with 92 additions and 68 deletions
|
|
@ -44,9 +44,7 @@ proc saveNodeData(node: TSNode): bool =
|
||||||
gStateRT.data.add(("function_declarator", ""))
|
gStateRT.data.add(("function_declarator", ""))
|
||||||
|
|
||||||
elif name in ["abstract_pointer_declarator"]:
|
elif name in ["abstract_pointer_declarator"]:
|
||||||
gStateRT.data[^1].val = "ptr " & gStateRT.data[^1].val.getIdentifier()
|
gStateRT.data.add(("pointer_declarator", ""))
|
||||||
if gStateRT.data[^1].val == "ptr char":
|
|
||||||
gStateRT.data[^1].val = "cstring"
|
|
||||||
elif name in ["field_declaration", "function_declarator"]:
|
elif name in ["field_declaration", "function_declarator"]:
|
||||||
gStateRT.data.add((name, ""))
|
gStateRT.data.add((name, ""))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,52 +24,37 @@ proc initGrammar() =
|
||||||
gStateRT.constStr &= &" {name}* = {val}\n"
|
gStateRT.constStr &= &" {name}* = {val}\n"
|
||||||
))
|
))
|
||||||
|
|
||||||
let typeGrammar = """
|
let
|
||||||
(type_qualifier?)
|
typeGrammar = """
|
||||||
(primitive_type|type_identifier?)
|
(type_qualifier?)
|
||||||
(sized_type_specifier?
|
(primitive_type|type_identifier?)
|
||||||
(primitive_type?)
|
(sized_type_specifier?
|
||||||
)
|
(primitive_type?)
|
||||||
(struct_specifier|union_specifier|enum_specifier?
|
)
|
||||||
(type_identifier)
|
(struct_specifier|union_specifier|enum_specifier?
|
||||||
)
|
(type_identifier)
|
||||||
"""
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
# typedef int X
|
paramListGrammar = &"""
|
||||||
# typedef X Y
|
(parameter_list
|
||||||
# typedef struct X Y
|
(parameter_declaration*
|
||||||
# typedef ?* Y
|
{typeGrammar}
|
||||||
gStateRT.grammar.add((&"""
|
(identifier|type_identifier?)
|
||||||
(type_definition
|
(pointer_declarator?
|
||||||
{typeGrammar}
|
(identifier|type_identifier)
|
||||||
(type_identifier?)
|
)
|
||||||
(pointer_declarator?
|
(abstract_pointer_declarator?)
|
||||||
(type_identifier)
|
)
|
||||||
)
|
)
|
||||||
)
|
"""
|
||||||
""",
|
|
||||||
proc (ast: ref Ast, node: TSNode) =
|
|
||||||
var
|
|
||||||
i = 0
|
|
||||||
typ = gStateRT.data[i].val.getIdentifier()
|
|
||||||
name = ""
|
|
||||||
tptr = ""
|
|
||||||
|
|
||||||
i += 1
|
funcGrammar = &"""
|
||||||
if i < gStateRT.data.len:
|
(function_declarator*
|
||||||
if gStateRT.data[i].name == "pointer_declarator":
|
(identifier|type_identifier)
|
||||||
tptr = "ptr "
|
{paramListGrammar}
|
||||||
i += 1
|
)
|
||||||
|
"""
|
||||||
name = gStateRT.data[i].val.getIdentifier()
|
|
||||||
|
|
||||||
if name notin gStateRT.types:
|
|
||||||
gStateRT.types.add(name)
|
|
||||||
if name == typ or typ == "object":
|
|
||||||
gStateRT.typeStr &= &" {name}* = object\n"
|
|
||||||
else:
|
|
||||||
gStateRT.typeStr &= &" {name}* = {tptr}{typ}\n"
|
|
||||||
))
|
|
||||||
|
|
||||||
template funcParamCommon(pname, ptyp, pptr, pout, count, i: untyped): untyped =
|
template funcParamCommon(pname, ptyp, pptr, pout, count, i: untyped): untyped =
|
||||||
ptyp = gStateRT.data[i].val.getIdentifier()
|
ptyp = gStateRT.data[i].val.getIdentifier()
|
||||||
|
|
@ -89,6 +74,64 @@ proc initGrammar() =
|
||||||
if ptyp != "object":
|
if ptyp != "object":
|
||||||
pout &= &"{pname}: {pptr}{ptyp},"
|
pout &= &"{pname}: {pptr}{ptyp},"
|
||||||
|
|
||||||
|
# typedef int X
|
||||||
|
# typedef X Y
|
||||||
|
# typedef struct X Y
|
||||||
|
# typedef ?* Y
|
||||||
|
gStateRT.grammar.add((&"""
|
||||||
|
(type_definition
|
||||||
|
{typeGrammar}
|
||||||
|
(type_identifier?)
|
||||||
|
(pointer_declarator?
|
||||||
|
(type_identifier!)
|
||||||
|
{funcGrammar}
|
||||||
|
)
|
||||||
|
{funcGrammar}
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
proc (ast: ref Ast, node: TSNode) =
|
||||||
|
var
|
||||||
|
i = 0
|
||||||
|
typ = gStateRT.data[i].val.getIdentifier()
|
||||||
|
name = ""
|
||||||
|
tptr = ""
|
||||||
|
|
||||||
|
i += 1
|
||||||
|
if i < gStateRT.data.len:
|
||||||
|
if gStateRT.data[i].name == "pointer_declarator":
|
||||||
|
tptr = "ptr "
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
if i < gStateRT.data.len:
|
||||||
|
name = gStateRT.data[i].val.getIdentifier()
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
if name notin gStateRT.types:
|
||||||
|
if i < gStateRT.data.len and gStateRT.data[^1].name == "function_declarator":
|
||||||
|
var
|
||||||
|
pout, pname, ptyp, pptr = ""
|
||||||
|
count = 1
|
||||||
|
|
||||||
|
while i < gStateRT.data.len:
|
||||||
|
if gStateRT.data[i].name == "function_declarator":
|
||||||
|
break
|
||||||
|
|
||||||
|
funcParamCommon(pname, ptyp, pptr, pout, count, i)
|
||||||
|
|
||||||
|
if pout.len != 0 and pout[^1] == ',':
|
||||||
|
pout = pout[0 .. ^2]
|
||||||
|
if typ != "object":
|
||||||
|
gStateRT.typeStr &= &" {name}* = proc({pout}): {tptr}{typ} {{.nimcall.}}\n"
|
||||||
|
else:
|
||||||
|
gStateRT.typeStr &= &" {name}*: proc({pout}) {{.nimcall.}}\n"
|
||||||
|
else:
|
||||||
|
gStateRT.types.add(name)
|
||||||
|
if name == typ or typ == "object":
|
||||||
|
gStateRT.typeStr &= &" {name}* = object\n"
|
||||||
|
else:
|
||||||
|
gStateRT.typeStr &= &" {name}* = {tptr}{typ}\n"
|
||||||
|
))
|
||||||
|
|
||||||
proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int) =
|
proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int) =
|
||||||
var
|
var
|
||||||
nname = name.getIdentifier()
|
nname = name.getIdentifier()
|
||||||
|
|
@ -163,23 +206,13 @@ proc initGrammar() =
|
||||||
gStateRT.typeStr &= &" {fname}*: proc({pout}) {{.nimcall.}}\n"
|
gStateRT.typeStr &= &" {fname}*: proc({pout}) {{.nimcall.}}\n"
|
||||||
i += 1
|
i += 1
|
||||||
else:
|
else:
|
||||||
gStateRT.typeStr &= &" {fname}*: {fptr}{ftyp}\n"
|
if ftyp == "object":
|
||||||
|
gStateRT.typeStr &= &" {fname}*: pointer\n"
|
||||||
|
else:
|
||||||
|
gStateRT.typeStr &= &" {fname}*: {fptr}{ftyp}\n"
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
let
|
let
|
||||||
paramListGrammar = &"""
|
|
||||||
(parameter_list
|
|
||||||
(parameter_declaration*
|
|
||||||
{typeGrammar}
|
|
||||||
(identifier?)
|
|
||||||
(pointer_declarator?
|
|
||||||
(identifier)
|
|
||||||
)
|
|
||||||
(abstract_pointer_declarator?)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
"""
|
|
||||||
|
|
||||||
fieldGrammar = &"""
|
fieldGrammar = &"""
|
||||||
(field_identifier!)
|
(field_identifier!)
|
||||||
(array_declarator!
|
(array_declarator!
|
||||||
|
|
@ -320,13 +353,6 @@ proc initGrammar() =
|
||||||
pEnumCommon(ast, node, gStateRT.data[^1].val, offset, 1)
|
pEnumCommon(ast, node, gStateRT.data[^1].val, offset, 1)
|
||||||
))
|
))
|
||||||
|
|
||||||
let funcGrammar = &"""
|
|
||||||
(function_declarator+
|
|
||||||
(identifier)
|
|
||||||
{paramListGrammar}
|
|
||||||
)
|
|
||||||
"""
|
|
||||||
|
|
||||||
# typ function(typ param1, ...)
|
# typ function(typ param1, ...)
|
||||||
gStateRT.grammar.add((&"""
|
gStateRT.grammar.add((&"""
|
||||||
(declaration
|
(declaration
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue