Пример #1
0
 protected virtual IPrimaryNode ParsePrimary(IPrimaryParentNode parent, Token token)
 {
     // PARSE: <primary> ::= identifier | <literal> | <block constructor> | ( '(' <expression> ')' )
     if (token is IdentifierToken)
     {
         return(new VariableReferenceleNode(parent, (IdentifierToken)token));
     }
     else if (Parser.IsBlockStartDelimiter(token))
     {
         return(this.ParseBlock(parent, (SpecialCharacterToken)token));
     }
     else if (Parser.IsOpeningParenthesis(token))
     {
         return(this.ParseParenthesizedExpression(parent, (SpecialCharacterToken)token));
     }
     else
     {
         return(this.ParseLiteral(parent, token));
     }
 }
Пример #2
0
        protected virtual ArrayLiteralNode ParseArrayLiteral(ILiteralNodeParent parent, SpecialCharacterToken arrayToken)
        {
            // PARSE: <array literal> ::= '#(' <array element>* ')'
            // <array element> ::= <literal> | identifier

            Token token = this.GetNextTokenxx(Preference.Default);

            if (!Parser.IsOpeningParenthesis(token))
            {
                this.ReportParserError(parent, SemanticErrors.MissingLiteralArrayOpeningParenthesis, token);
                // The hash token is thrown away and lost :-/   ... only the current token is passed on.
                this.ResidueToken = token;
                return(null);
            }
            List <LiteralNode> elements = new List <LiteralNode>();
            ArrayLiteralNode   result   = new ArrayLiteralNode(parent, arrayToken, (SpecialCharacterToken)token);

            // Process tokens inside the array ...
            while (true)
            {
                // ... get next token in the array ...
                token = this.GetNextTokenxx(Preference.NegativeSign | Preference.UnquotedSelector);

                // Is this closing parenthesis?
                if (Parser.IsClosingParenthesis(token))
                {
                    // Closing parenthesis ... done with the array, return litral array node.
                    result.SetContents(elements, (SpecialCharacterToken)token);
                    this.ResidueToken = null;
                    return(result);
                }

                if (token is EofToken)
                {
                    // Unfinished source code ... return
                    result.SetContents(elements, null);
                    this.ReportParserError(parent, SemanticErrors.MissingLiteralArrayClosingParenthesis, token);
                    this.ResidueToken = token;
                    return(result);
                }

                // PARSE: <array element> ::= <literal> | identifier
                if (token is ILiteralArrayIdentifierToken)
                {
                    // identifier ... special case only inside arrays.
                    elements.Add(new IdentifierLiteralNode(result, (ILiteralArrayIdentifierToken)token));
                }
                else
                {
                    // ... it's not identifier, so it must be an literal
                    LiteralNode element = this.ParseLiteral(result, token);
                    if (element == null)
                    {
                        // Report error in source code ... here, it must be a literal
                        this.ReportParserError(result, SemanticErrors.UnrecognizedLiteral, token);
                        result.SetContents(elements, null);
                        return(result);
                    }
                    else
                    {
                        // ... add the child element to the array elements.
                        elements.Add(element);
                    }
                }
            }
        }