public override bool Equals(ITypeSpecifier other) { AtomicTypeSpecifier o = other as AtomicTypeSpecifier; if (o == null) { return(false); } return((o.TypeName.ToLower() == this.TypeName.ToLower()) && (o.Unsigned == this.Unsigned)); }
public override bool CanImplicitCastFrom(ITypeSpecifier type) { switch (TypeName) { case "void": return(false); case "char": case "short": case "int": case "long": switch (type.TypeName) { case "char": case "short": case "int": case "long": return(true); default: return(false); } case "float": case "double": switch (type.TypeName) { case "float": case "double": return(true); default: return(false); } default: return(false); } }
public FloatingConstant (ISyntaxNode parent, ref string Input) : base (parent) { Pattern regExPattern = "^\\s*" + new Group("def", new Group("signess", "[+-]") + "?" + new Group("nums", new Group("pre_comma", "\\d+") + "(\\." + new Group("post_comma", "\\d+") + ")?([eE]" + new Group("exp", "-?\\d+") + ")?")); 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); string value = match.Groups["nums"].Value; try { Value = Convert.ToDouble(value); I_Type = AtomicTypeSpecifier.Double(this); } catch (OverflowException) { throw new SyntaxException("syntax error: value \"" + value + "\" is too large for a floating value."); } }
public VariableDeclaration(ISyntaxNode parent, ref string Input, bool allowAssignment) : base(parent) { // string regExPattern = "(?<def>(?<signdness>signed|unsigned)?\\s*(?<type>(void|char|short|int|long|float|double))(?<pointer>[\\s\\*]+)(?<identifier>[a-zA-Z_][a-zA-Z_0-9]*)\\s*(=\\s*(?<assignment>.*))?);(?<rest>.*)"; Pattern regExPattern = "^\\s*" + new Group("def", "(" + new Group("signedness", "signed|unsigned") + "\\s+" + ")?" + new Group("type", Provider.type) + new Group("pointer", "[\\s\\*]+") + new Group("identifier", Provider.identifier) + "\\s*(" + "=\\s*" + new Group("assignment", ".*") + ")?") + ";"; 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... // Load signedness if (match.Groups["signedness"].Success) { this.Signedness = (Signedness)Enum.Parse(typeof(Signedness), match.Groups["signedness"].Value, true); } else { this.Signedness = this.DefaultSignedness; } // Load type Type = ITypeSpecifier.Parse(this, match.Groups["type"].Value); if (Type == null) { throw new SyntaxException("Error parsing variable: Expected type, got \"" + match.Groups["type"].Value + "\"."); } // Load identifier Identifier = match.Groups["identifier"].Value; if ((match.Groups["identifier"].Success) && ((Identifier == null) || (Identifier == ""))) { throw new SyntaxException("Error parsing variable: Expected identifier, got \"" + match.Groups["identifier"].Value + "\"."); } // And last but not least possible assignments if (allowAssignment) { Assignment = match.Groups["assignment"].Value; if ((match.Groups["assignment"].Success) && ((Assignment == null) || (Assignment == ""))) { throw new SyntaxException("Error parsing variable: Expected assignment, got \"" + match.Groups["assignment"].Value + "\"."); } } else if (match.Groups["assignment"].Success) { throw new SyntaxException("Error parsing variable: Assignment is not allowed here."); } }
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; } }
public IntegerConstant(ISyntaxNode parent, ref string Input) : base(parent) { Pattern regExPattern = "^\\s*" + new Group("def", new Group("signess", "[+-]") + "?" + new Group("base", "0[xbo]") + "?" + new Group("nums", "\\d+")); 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); string value = match.Groups["nums"].Value; int numBase = 10; switch (match.Groups["base"].Value) { case "": numBase = 10; break; case "0x": case "0X": numBase = 16; break; case "0o": case "oO": numBase = 8; break; case "0b": case "0B": numBase = 2; break; } try { Value = Convert.ToInt32(value, numBase); if (match.Groups["signess"].Value == "-") { Value = (Int32)Value * -1; } I_Type = AtomicTypeSpecifier.Int(this); } catch (OverflowException) { try { Value = Convert.ToInt64(value, numBase); if (match.Groups["signess"].Value == "-") { Value = (Int64)Value * -1; } I_Type = AtomicTypeSpecifier.Long(this); } catch (OverflowException) { try { Value = Convert.ToUInt64(value); if (match.Groups["signess"].Value == "-") { throw new SyntaxException("syntax error: value \"" + value + "\" is too large for a signed integer value."); } I_Type = AtomicTypeSpecifier.ULong(this); } catch (OverflowException) { throw new SyntaxException("syntax error: value \"" + value + "\" is too large for a integer value."); } } } }