如何查找源C#代码中的所有变量?
这是我的非工作语法,因为ZeroOrMore(VarDef4)
Protected = Literal("protected")
Private = Literal("private")
Public = Literal("public")
Modification = Public^Private^Protected
Static = Literal("static")
Auto = Literal("Auto")
MemoryType =(Static^Auto)
Bool = Literal("bool"); Byte = Literal("byte"); Char = Literal("char"); Decimal = Literal("decimal"); Double = Literal("double"); Enum = Literal("enum")
Float = Literal("float"); Int = Literal("int"); Long = Literal ("long"); Sbyte = Literal("sbyte"); Short = Literal("short")
Struct = Literal("struct"); String = Literal("string"); Uint = Literal("uint"); Ulong = Literal("ulong"); Ushort = Literal("ushort")
VariableType = (Bool)^(Byte)^(Char)^(Decimal)^(Double)^(Enum)^(Float)^(Int)^(Long)^(Sbyte)^(Short)^(Struct)^(String)^(Uint)^(Ulong)^(Ushort)
Variable = Word(alphanums + '_' + '$'+ '.' )
VarDef2 = '=' + OneOrMore(Word(alphanums + '_' + '$' + '"' + '-'+ "'" ) )
VarDef3 = ZeroOrMore("," + Variable + Optional(VarDef2))
VarDef4 = VarDef2^VarDef3 VarDef = Optional(Modification) + Optional(MemoryType) + (VariableType) + Variable + ZeroOrMore(VarDef4) + ";"
例如我给出的字符串输入:
int k1 = 89, k2 = 6;
public static int alpha = 54 ;
在输出文件中:
{'Row': 1, 'Construct': "['int', 'k1', '89]", "['int', 'k2', '6']", 'Variable' : k1, k2, 'VariableType': int, 'Modification': not defined, 'MemoryType': not defined, 'Line': 'int k1 = 89, k2 = 6'}
{'Row': 2, 'Construct': "['public', 'static', 'int', 'alpha']", 'Variable': alpha, 'VariableType': int,'Modification': public, 'MemoryType': static, 'Line': 'public static int alpha = 54'}
也许有另一种方法可以实现这种语法或如何制作这种语法?