public Assignment(Document parent, ref string Input) : base(parent) { Pattern regExPattern = "^\\s*" + new Group("def", new Group("identifier", Provider.identifier) + "\\s*= " + new Group("expression", ".*")) + ";"; System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(regExPattern); System.Text.RegularExpressions.Match match = regEx.Match(Input); if (!match.Success) throw new ParseException(); // if (match.Index != 0) // throw new ParseException(); Input = Input.Remove(0, match.Length); // Also removes all starting spaces etc... Identifier = match.Groups["identifier"].Value; // Expression = match.Groups["identifier"].Value; }
public Program(string[] arg) { /* Pattern regExPattern = "^\\s*" + new Group("def", new Group("identifier", Provider.identifier) + "\\s*\\(" + new Group("params", "[a-zA-Z_0-9*\\+/!&|%()=,\\s]*") + "\\)\\s*") + ";";*/ // Pattern regExPattern = new Group("operator", "\\+|-|/|\\*|==|!=|>=|<=|>|<|\\||\\|\\|"); Pattern regExPattern = "^\\s*" + new Group("def", "(\\+|\\*)"); System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(regExPattern); System.Text.RegularExpressions.Match match = regEx.Match(" tetsfunc(2134, 1412);"); if (!match.Success) Console.WriteLine ("No match found."); else { foreach (String gname in regEx.GetGroupNames()) { if (match.Groups[gname].Success) Console.WriteLine(gname + ": "+match.Groups[gname]); else Console.WriteLine(gname + ": not found"); } } Console.WriteLine(); Console.WriteLine(); // Console.ReadKey(); // return; //Int32 foo = 10.2e12; string test = File.ReadAllText ("test.c"); Document doc = new Document(test); foreach (FunctionDeclaration node in doc.Functions.Values) { Console.WriteLine("function "+node.Identifier); if (node.HasBody) { foreach (ISyntaxNode child in node.Body) { Console.WriteLine(child.ToString()); } } }/* foreach (VariableDeclaration node in doc.Variables.Values) { Console.WriteLine("variable " + node.Identifier); } foreach (ConstantDeclaration node in doc.Constants.Values) { Console.WriteLine("constant " + node.Identifier); }*/ Console.ReadKey(); }
public FunctionDeclaration(Document parent, ref string Input) : base(parent) { Pattern regExPattern = "^\\s*" + new Group("def", new Group("type", Provider.type) + "\\s+" + new Group("identifier", Provider.identifier) + "\\s*\\(" + new Group("params", "\\s*" + Provider.type + "\\s*" + Provider.identifier + "\\s*(,\\s*" + Provider.type + "\\s*" + Provider.identifier + ")*") + "?" + "\\)\\s*") + new Group("terminus", "[;{]"); System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(regExPattern); System.Text.RegularExpressions.Match match = regEx.Match(Input); if (!match.Success) throw new ParseException(); //if (match.Index != 0) // throw new ParseException(); Input = Input.Remove(0, match.Index+match.Length); // Also removes all starting spaces etc... Type = ITypeSpecifier.Parse(this, match.Groups["type"].Value); if (Type == null) throw new SyntaxException("Error parsing variable: Expected type, got \"" + match.Groups["type"].Value + "\"."); Identifier = match.Groups["identifier"].Value; if ((Identifier == null) || (Identifier == "")) throw new SyntaxException("Error parsing variable: Expected identifier, got \"" + match.Groups["identifier"].Value + "\"."); string[] paramStrings = match.Groups["params"].Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); List<VariableDeclaration> param = new List<VariableDeclaration>(); for (int i = 0; i < paramStrings.Length; i++) { paramStrings[i] = paramStrings[i].Trim(new char[] { ' ', '\t', '\n', '\r' }); try { VariableDeclaration decl = new VariableDeclaration(this, ref paramStrings[i], false); param.Add(decl); } catch (ParseException) { } } Parameters = param.ToArray(); if (match.Groups["terminus"].Value == "{") // Well, there's a body... { //TODO: Parse body of function... //Body = new ISyntaxNode[0]; List<ISyntaxNode> bodyNodes = new List<ISyntaxNode>(); System.Text.RegularExpressions.Regex endMatch = new System.Text.RegularExpressions.Regex("^\\s*}"); while (!endMatch.IsMatch(Input)) { FunctionCall fcall = TryParse<FunctionCall>(this, ref Input); if (fcall != null) { bodyNodes.Add(fcall); // Search for following semikolon and remove it... System.Text.RegularExpressions.Regex semikolon = new System.Text.RegularExpressions.Regex("^\\s*;"); System.Text.RegularExpressions.Match semikolonMatch = semikolon.Match(Input); if (!semikolonMatch.Success) throw new SyntaxException("Syntax error: Missing semikolon after function call."); Input = Input.Remove(0, semikolonMatch.Length); continue; } throw new SyntaxException("Syntax error: Invalid token \"" + Input + "\""); } Body = bodyNodes.ToArray(); // Finally remove remaining (closing) } int index = Input.IndexOf('}'); Input = Input.Remove(0, index+1); } else if (match.Groups["terminus"].Value == ";") // No Body, only header declaration Body = null; }