Example #1
0
        public static IRightValue Parse(ISyntaxNode parent, ref string Input)
        {
            string temp = Input;

            System.Text.RegularExpressions.Regex endRegEx      = new System.Text.RegularExpressions.Regex("^\\s*$");
            System.Text.RegularExpressions.Regex bracketsRegEx = new System.Text.RegularExpressions.Regex("^\\s*\\)\\s*");
            System.Text.RegularExpressions.Regex commaRegEx    = new System.Text.RegularExpressions.Regex("^\\s*,\\s*");

            IRightValue highestNode = null;

            while ((!endRegEx.IsMatch(Input)) && (!bracketsRegEx.IsMatch(Input)) && (!commaRegEx.IsMatch(Input)))
            {
                IntegerConstant iconst = TryParse <IntegerConstant>(parent, ref Input);
                if (iconst != null)
                {
                    if (highestNode != null)                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Invalid rvalue before integer constant.");
                    }
                    highestNode = iconst;
                    continue;
                }

                FloatingConstant fconst = TryParse <FloatingConstant>(parent, ref Input);
                if (fconst != null)
                {
                    if (highestNode != null)                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Invalid rvalue before floating constant.");
                    }
                    highestNode = fconst;
                    continue;
                }

                FunctionCall fcall = TryParse <FunctionCall>(parent, ref Input);
                if (fcall != null)
                {
                    if (highestNode != null)                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Invalid rvalue before function call.");
                    }
                    highestNode = fcall;
                    continue;
                }


                //string tmp = Input;
                IBinaryOperator binop = IBinaryOperator.Parse(parent, ref Input, highestNode);
                if (binop != null)
                {
                    //	Input = tmp;

                    if (highestNode == null)                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Missing first operand for binary operator.");
                    }
                    highestNode = binop;
                    continue;
                }

                IUnaryOperator unop = IUnaryOperator.Parse(parent, ref Input, highestNode);
                if (unop != null)
                {
                    if ((unop.Position == OperatorPosition.Postfix) && (highestNode == null))                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Missing first operand for unary operator.");
                    }
                    highestNode = unop;
                    continue;
                }

                Brackets backets = TryParse <Brackets>(parent, ref Input);
                if (backets != null)
                {
                    if (highestNode != null)                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Invalid rvalue before brackets.");
                    }
                    highestNode = backets;
                    continue;
                }

                //	InfixOperator iopp = TryParse<InfixOperator>(ref Input, delegate(ref string i) { return new InfixOperator(parent, ref i, highestNode); });
                //	if (iopp != null)
                //		highestNode = fcall;

                // Well, if nothing got parsed, then it's a invalid expression
                throw new SyntaxException("Syntax error: Invalid token \"" + Input + "\"");
            }

            if ((highestNode is IOperator) &&
                ((highestNode as IOperator).SecondaryOperand is IOperator) &&
                (highestNode.Priority < (highestNode as IOperator).SecondaryOperand.Priority))
            {
                IOperator higher = (highestNode as IOperator);
                IOperator lower  = (IOperator)higher.SecondaryOperand;

                higher.SecondaryOperand = lower.PrimaryOperand;
                lower.PrimaryOperand    = higher;
                higher = lower;

                highestNode = higher;
            }


            return(highestNode);
        }
Example #2
0
        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;
            }
        }