Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your main goal is to master the basics of formal languages. A good start-up might be found <a href="http://intellect.dk/post/Writing-a-calculator-in-C-using-Irony.aspx" rel="nofollow">here</a>. This article describes the way to use Irony on the sample of a grammar of a simple numeric calculator.</p> <p>Suppose you want to parse a certain file containing C# code the path to which you know:</p> <pre><code>private void ParseForLongMethods(string path) { _parser = new Parser(new CSharpGrammar()); if (_parser == null || !_parser.Language.CanParse()) return; _parseTree = null; GC.Collect(); //to avoid disruption of perf times with occasional collections _parser.Context.SetOption(ParseOptions.TraceParser, true); try { string contents = File.ReadAllText(path); _parser.Parse(contents);//, "&lt;source&gt;"); } catch (Exception ex) { } finally { _parseTree = _parser.Context.CurrentParseTree; TraverseParseTree(); } } </code></pre> <p>And here is the traversal method itself with counting some info in the nodes. Actually this code counts the number of statements in every method of the class. If you have any question you are always welcome to ask me</p> <pre><code> private void TraverseParseTree() { if (_parseTree == null) return; ParseNodeRec(_parseTree.Root); } private void ParseNodeRec(ParseTreeNode node) { if (node == null) return; string functionName = ""; if (node.ToString().CompareTo("class_declaration") == 0) { ParseTreeNode tmpNode = node.ChildNodes[2]; currentClass = tmpNode.AstNode.ToString(); } if (node.ToString().CompareTo("method_declaration") == 0) { foreach (var child in node.ChildNodes) { if (child.ToString().CompareTo("qual_name_with_targs") == 0) { ParseTreeNode tmpNode = child.ChildNodes[0]; while (tmpNode.ChildNodes.Count != 0) { tmpNode = tmpNode.ChildNodes[0]; } functionName = tmpNode.AstNode.ToString(); } if (child.ToString().CompareTo("method_body") == 0) //method_declaration { int statementsCount = FindStatements(child); //Register bad smell if (statementsCount&gt;(((LongMethodsOptions)this.Options).MaxMethodLength)) { //function.StartPoint.Line int functionLine = GetLine(functionName); foundSmells.Add(new BadSmellRegistry(name, functionLine,currentFile,currentProject,currentSolution,false)); } } } } foreach (var child in node.ChildNodes) { ParseNodeRec(child); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload