Ejemplo n.º 1
0
        ExpSyntaxTree ParseMainExp()
        {
            ExpSyntaxTree exp;

            switch (LookAhead().m_type)
            {
            case (int)TokenType.NIL:
            case (int)TokenType.FALSE:
            case (int)TokenType.TRUE:
            case (int)TokenType.NUMBER:
            case (int)TokenType.STRING:
            case (int)TokenType.DOTS:
            case (int)TokenType.NAME:
                exp = new Terminator(NextToken());
                break;

            case (int)TokenType.STRING_BEGIN:
                exp = ParseComplexString();
                break;

            case (int)TokenType.FUNCTION:
                exp = ParseFunctionDef();
                break;

            case (int)'(':
                NextToken();
                exp = ParseExp();
                if (NextToken().m_type != (int)')')
                {
                    throw NewParserException("expect ')' to match Main Exp's head '('", _current);
                }
                break;

            case (int)'{':
                exp = ParseTableConstructor();
                break;

            // unop exp priority is 90 less then ^
            case (int)'-':
            case (int)TokenType.NOT:
                var unexp = new UnaryExpression(LookAhead().m_line);
                unexp.op  = NextToken();
                unexp.exp = ParseExp(90);
                exp       = unexp;
                break;

            default:
                throw NewParserException("unexpect token for main exp", _look_ahead);
            }
            return(ParseTailExp(exp));
        }
Ejemplo n.º 2
0
        private ComplexString ParseComplexString()
        {
            var head = NextToken();
            var next = LookAhead();

            var exp = new ComplexString(_current.m_line);

            if (head.m_string_type >= StringBlockType.InverseQuotation)
            {
                exp.is_shell = true;
                if (next.Match(TokenType.STRING) &&
                    next.m_string.Length > 3 &&
                    head.m_string_type == StringBlockType.InverseThreeQuotation)
                {
                    int idx = 0;
                    var str = next.m_string;
                    while (idx < str.Length && char.IsLetter(str[idx]))
                    {
                        idx++;
                    }
                    if (idx < str.Length && str[idx] == ' ')
                    {
                        exp.shell_name = str.Substring(0, idx);
                        next.m_string  = str.Substring(idx + 1);
                    }
                }
            }

            do
            {
                next = NextToken();
                if (next.Match(TokenType.STRING) || next.Match(TokenType.NAME))
                {
                    var term = new Terminator(next);
                    exp.list.Add(term);
                }
                else if (next.Match('{'))
                {
                    var term = ParseComplexItem();
                    exp.list.Add(term);
                }
                else
                {
                    throw NewParserException("expect string,name,'{' in complex-string", next);
                }
            } while (next.IsStringEnded == false);


            return(exp);
        }