public override void EnterMethod_declaration(CSharpParser.Method_declarationContext context) { Console.WriteLine("Entering method_declaration context."); // Getting the current scope node and current modifiers: Symbol.ModifierFlag modFlags = TreatModTokens(); modifiersTokens.Clear(); Node currentScopeNode = ast.GetNode(symbolTable.CurrentScopeNode); ClassSymbol ownerSymbol = (ClassSymbol)(currentScopeNode.Data); MethodSymbol methodSymbol = new MethodSymbol(modFlags, ownerSymbol); IToken idToken = context.method_member_name().identifier(0).Start; symbolTable.AddSymbol(idToken, methodSymbol); // Creating the method AST node: Node methodNode = new Node(idToken, Node.Kind.MethodDeclaration, currentType, methodSymbol); ast.AddNode(methodNode); int methodIndex = ast.NodeIndex(methodNode); currentScopeNode.AddChildIndex(methodIndex); // Entering the method scope: symbolTable.EnterScope(methodIndex); // Creating a node for each parameter: CSharpParser.Formal_parameter_listContext parameters = context.formal_parameter_list(); if (parameters != null) { CSharpParser.Fixed_parameterContext[] fixedParams = parameters.fixed_parameters().fixed_parameter(); foreach (CSharpParser.Fixed_parameterContext parameter in fixedParams) { CSharpParser.Arg_declarationContext arg = parameter.arg_declaration(); if (arg != null) { CSharpParser.Type_Context typeContext = arg.type_(); IToken varToken = arg.identifier().Start; Type t = TreatTypeContext(typeContext); VariableSymbol varSymbol = new VariableSymbol(Symbol.ModifierFlag.None, methodSymbol); symbolTable.AddSymbol(varToken, varSymbol); Node varNode = new Node(varToken, Node.Kind.MethodParameter, t, null); ast.AddNode(varNode); int varIndex = ast.NodeIndex(varNode); methodNode.AddChildIndex(varIndex); } } } // Creating the subtree for the method implementation: CSharpParser.BlockContext blockContext = context.method_body().block(); if (blockContext != null) { TreatBlock(blockContext, methodNode); } }
public FormalParameterList ParseFormalParameterList( BufferedTokenStream tokenStream, CSharpParser.Formal_parameter_listContext formalParameterList) { FormalParameterList formalParamList = null; if (formalParameterList != null) { formalParamList = new FormalParameterList(); var fixedParameters = formalParameterList?.fixed_parameters()?.fixed_parameter(); if (fixedParameters != null) { foreach (var fixedParameter in fixedParameters) { formalParamList.FixedParameters.Add( new FixedParameter() { Attributes = GetTextWithWhitespace(tokenStream, fixedParameter?.attributes()), ParameterModifier = fixedParameter?.parameter_modifier()?.GetText(), Type = GetTextWithWhitespaceMinifiedLite(tokenStream, fixedParameter.type_()), Identifier = fixedParameter.identifier().GetText(), DefaultArgument = GetTextWithWhitespace( tokenStream, fixedParameter?.default_argument()?.expression()) } ); } } var parameterArray = formalParameterList?.parameter_array(); if (parameterArray != null) { formalParamList.ParameterArray = new ParameterArray() { Attributes = GetTextWithWhitespace(tokenStream, parameterArray?.attributes()), Type = GetTextWithWhitespaceMinifiedLite(tokenStream, parameterArray.array_type()), Identifier = parameterArray.identifier().GetText() }; } } return(formalParamList); }
public override void EnterFormal_parameter_list([NotNull] CSharpParser.Formal_parameter_listContext context) { ParserRuleContext ancesstorOfTree = (ParserRuleContext)context.Parent; if (!(ancesstorOfTree is CSharpParser.Method_declarationContext)) { return; } int childLength = context.GetChild(0).ChildCount; string type = ""; string varId = ""; for (int i = 0; i < childLength; i = i + 2) { IParseTree childTree = context.GetChild(0).GetChild(i).GetChild(0); if (childTree is CSharpParser.Arg_declarationContext) { for (int j = 0; j < childTree.ChildCount; j++) { if (childTree.GetChild(j) is CSharpParser.TypeContext) { type = childTree.GetChild(j).GetText(); } else if (childTree.GetChild(j) is CSharpParser.IdentifierContext) { varId = childTree.GetChild(j).GetText(); } } if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(varId)) { return; } VariableDefine variableDefine = new VariableDefine(type, varId); if (listVariable != null) { if (!(listVariable.Any(x => x.name == variableDefine.name))) { listVariable.Add(variableDefine); } } else { listVariable = new List <VariableDefine>(); listVariable.Add(variableDefine); } methodInfor = new MethodInfor(); List <string> paramx = new List <string>(); foreach (var item in listVariable) { paramx.Add(item.name); } methodInfor.listArgs = paramx; int startLine = ancesstorOfTree.Start.Line; methodInfor.startLine = startLine; ParserRuleContext parrentOfTree = (ParserRuleContext)childTree.Parent; while (!(parrentOfTree is CSharpParser.Method_declarationContext) && !(parrentOfTree is CSharpParser.Constructor_declarationContext)) { try { parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; if (parrentOfTree == null) { return; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (parrentOfTree is CSharpParser.Method_declarationContext) { methodInfor.methodName = parrentOfTree.GetChild(0).GetText(); } else if (parrentOfTree is CSharpParser.Constructor_declarationContext) { methodInfor.methodName = parrentOfTree.GetChild(0).GetText(); } parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; if (parrentOfTree is CSharpParser.Typed_member_declarationContext) { methodInfor.outputType = parrentOfTree.GetChild(0).GetText(); } parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; while (!(parrentOfTree is CSharpParser.Class_definitionContext)) { try { parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; if (parrentOfTree == null) { return; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (parrentOfTree is CSharpParser.Class_definitionContext) { classTree = parrentOfTree; methodInfor.className = parrentOfTree.GetChild(1).GetText(); } } } }
public override void EnterFormal_parameter_list([NotNull] CSharpParser.Formal_parameter_listContext context) { ParserRuleContext ancesstor = (ParserRuleContext)context.Parent; //for (int i = 0; i < context.ChildCount; i++) //{ // Console.WriteLine("Child "+i+": " + context.GetChild(i).GetText()); // for (int j = 0; j < context.GetChild(i).ChildCount; j++) // { // Console.WriteLine("Child " + i + "." + j + ": " + context.GetChild(i).GetChild(j).GetText()); // } //} //context = context.GetChild(0); int childLength = context.GetChild(0).ChildCount; string type = ""; string varId = ""; for (int i = 0; i < childLength; i = i + 2) { IParseTree childTree = context.GetChild(0).GetChild(i).GetChild(0); //Console.Out.WriteLine(childTree.GetText()); //Console.WriteLine(childTree.ChildCount); //Console.WriteLine(childTree.GetType().ToString()); if (childTree is CSharpParser.Arg_declarationContext) { for (int j = 0; j < childTree.ChildCount; j++) { if (childTree.GetChild(j) is CSharpParser.TypeContext) { type = childTree.GetChild(j).GetText(); Console.WriteLine(type); } else if (childTree.GetChild(j) is CSharpParser.IdentifierContext) { varId = childTree.GetChild(j).GetText(); Console.WriteLine(varId); } } } /* * for (int j = 0; j < childTree.ChildCount; j++) * { * //if(childTree.GetChild(j) is CSharpParser.TypeContext) * //{ * // type = childTree.GetChild(j).GetText(); * // Console.Out.WriteLine(type); * //}else if(childTree.GetChild(j) is CSharpParser.Variable_declaratorContext) * //{ * // varId = childTree.GetChild(j).GetText(); * // Console.Out.WriteLine(varId); * //} * * Console.WriteLine(childTree.GetChild(j).GetText()); * Console.WriteLine(childTree.GetChild(j).ChildCount); * for(int k = 0; k < childTree.GetChild(j).ChildCount; k++) * { * Console.WriteLine(childTree.GetChild(j).GetChild(k).GetText()); * Console.WriteLine(childTree.GetChild(j).GetChild(k).GetType().ToString()); * } * * }*/ /* * ParserRuleContext parrentOfTree = (ParserRuleContext)context.Parent; * while (!(parrentOfTree is CSharpParser.Method_declarationContext) && !(parrentOfTree is CSharpParser.Constructor_declarationContext)) * { * try * { * parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; * if (parrentOfTree == null) { return; } * } * catch (Exception ex) * { * Console.WriteLine(ex.Message); * } * } * if (parrentOfTree is CSharpParser.Method_declarationContext) * { * //for (int k = 0; k < parrentOfTree.Parent.ChildCount; k++) * //{ * Console.WriteLine("Method name: " + parrentOfTree.GetChild(0).GetText()); * Console.WriteLine("output type: " + parrentOfTree.Parent.GetChild(0).GetText()); * Console.WriteLine(parrentOfTree.Parent.Parent.Parent.Parent.Parent.Parent.GetType().ToString()); * //} * * } * parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; * while (!(parrentOfTree is CSharpParser.Class_definitionContext)) * { * try * { * parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; * if (parrentOfTree == null) { return; } * } * catch (Exception ex) * { * Console.WriteLine(ex.Message); * } * } * //Console.WriteLine(parrentOfTree.GetType().ToString()); * Console.WriteLine("Class name: " + parrentOfTree.GetChild(1).GetText()); */ } }