Ejemplo n.º 1
0
        public static bool Matches(DeclarationTerm term, Ast.Node node)
        {
            if (term.Type == DeclarationTermType.Keyword || term.Type == DeclarationTermType.Operator)
            {
                if (node is Ast.Identifier)
                    return term.Name == (node as Ast.Identifier).Name.Value.ToUpper();
                else if (node is Ast.MemberAccess.DummyKeyword)
                    return term.Name == (node as Ast.MemberAccess.DummyKeyword).Name;
                return false;
            }

            return true;
        }
Ejemplo n.º 2
0
        internal static DeclarationTerm ParseDeclarationTerm(TokenStream Stream)
        {
            DeclarationTerm r = null;
            var start = Stream.Next();

            if (Stream.Next().Type == TokenType.Identifier)
            {
                r = new DeclarationTerm
                {
                    Name = Stream.Next().Value.ToUpper(),
                    Type = DeclarationTermType.Keyword,
                    RepetitionType = DeclarationTermRepetitionType.Once
                };
                Stream.Advance();
            }
            else if (Stream.Next().Type == TokenType.Operator)
            {
                r = new DeclarationTerm
                {
                    Name = Stream.Next().Value.ToUpper(),
                    Type = DeclarationTermType.Operator,
                    RepetitionType = DeclarationTermRepetitionType.Once
                };
                Stream.Advance();
            }
            else if (Stream.Next().Type == TokenType.OpenParen)
            {
                Stream.Advance();
                if (Stream.Next().Type != TokenType.Identifier)
                    throw new CompileError("[021] Expected identifier", start);
                r = new DeclarationTerm
                {
                    Name = Stream.Next().Value.ToUpper(),
                    Type = DeclarationTermType.Term,
                    RepetitionType = DeclarationTermRepetitionType.Once
                };
                Stream.Advance();
                if (Stream.Next().Type == TokenType.Colon)
                {
                    Stream.Advance();
                    if (Stream.Next().Type != TokenType.Identifier)
                        throw new CompileError("[022] Expected identifier", start);
                    var declaredType = Stream.Next().Value;
                    r.DeclaredTypeName = declaredType.ToUpper();
                    Stream.Advance();
                }
                if (Stream.Next().Type != TokenType.CloseParen)
                    throw new CompileError("[023] Expected )", start);
                Stream.Advance();
            }
            else
                throw new CompileError("[025] Illegal token in declaration header", start);

            if (!Stream.AtEnd())
            {
                if (Stream.Next().Type == TokenType.QuestionMark)
                {
                    var marker = Stream.Next();
                    var repetitionMarker = Stream.Next().Value;
                    Stream.Advance();
                    if (repetitionMarker == "?")
                    {
                        if (r.Type != DeclarationTermType.Keyword)
                            throw new CompileError("[026] Only keywords can be optional in a declaration header", Stream);
                        r.RepetitionType = DeclarationTermRepetitionType.Optional;
                    }
                        //Left over from when terms could be repeated.
                    //else if (repetitionMarker == "+")
                    //    r.RepetitionType = DeclarationTermRepetitionType.OneOrMany;
                    //else if (repetitionMarker == "*")
                    //    r.RepetitionType = DeclarationTermRepetitionType.NoneOrMany;
                    else
                        throw new CompileError("[027] Unrecognized repetition marker on declaration term", marker);
                }
            }

            return r;
        }