Пример #1
0
        TsFunctionDecl ParseTsFunction(ref TokenPointer tp)
        {
            var func = new TsFunctionDecl {
                Token = tp
            };

            if (tp.Value.IsIdentifier("static")) // static extend(properties: any, classProperties?: any): any;
            {
                func.IsStatic = true;
                tp            = tp.Next();
            }
            if (tp.Value.IsIdentifier()) //doesn't happen at: export function (booleanValue: bool, message?: string);
            {
                func.Name = tp.Value.Text;
                tp        = tp.Next();
            }

            TokenPointer optional = null;

            if (tp.Value.IsOperator("?"))
            {
                optional        = tp;
                tp              = tp.Next();
                func.IsOptional = true;
            }

            tp = ParseBracketParametersAndReturnType(tp, func, "(", ")");
            return(func);
        }
Пример #2
0
 TsMemberDecl ParseTsMember(ref TokenPointer tp)
 {
     if (tp.Value.IsOperator("["))
     {
         return(ParseTsIndexer(ref tp));
     }
     else if (tp.Value.IsOperator("("))
     {
         var func = new TsFunctionDecl();
         tp = ParseBracketParametersAndReturnType(tp, func, "(", ")");
         return(func);
     }
     tp = tp.Verify(t => t.IsIdentifier());
     if (tp.Next().Value.IsOperator(";"))    //export function listeners(event: string): { Function; }[];
     {
         return(ParseTsField(ref tp));
     }
     else if (tp.Next().Value.IsOperator(":") || tp.Next(2).Value.IsOperator(":"))
     {
         return(ParseTsField(ref tp));
     }
     else
     {
         return(ParseTsFunction(ref tp));
     }
 }
Пример #3
0
        TsTypeRef ParseTypeRef(ref TokenPointer tp)
        {
            TsTypeRef tr;

            if (tp.Value.IsOperator("("))
            {
                //delegate
                var func = new TsFunctionDecl {
                    IsDelegate = true
                };
                tp        = ParseBracketParameters(tp, func, "(", ")");
                tp        = tp.Verify(t => t.Is(TokenTypes.LambdaOperator)).Next(); //=>
                func.Type = ParseTypeRef(ref tp);
                tr        = new TsDelegateTypeRef {
                    Decl = func
                };
            }
            else if (tp.Value.IsOperator("{"))
            {
                var tr2 = new TsInterfaceDeclRef {
                    Token = tp, Decl = new TsTypeDecl()
                };
                tp = ParseBracesAndMembers(tp, tr2.Decl);
                tr = tr2;
            }
            else
            {
                tp = tp.Verify(t => t.IsIdentifier());
                var typeRef = new TsNamedTypeRef {
                    Token = tp, Name = tp.Value.Text
                };
                tr = typeRef;
                tp = tp.Next();
                var previous = typeRef;
                while (tp.Value.IsOperator("."))
                {
                    tp = tp.Next().Verify(t => t.IsIdentifier());
                    var me = new TsMemberTypeRef {
                        Name = tp.Value.Text, Previous = previous
                    };
                    previous = me;
                    tr       = me;
                    tp       = tp.Next();
                }
            }
            if (tp.Value.IsOperator("[") && tp.Next().Value.IsOperator("]"))
            {
                tp         = tp.Next();
                tp         = tp.Next();
                tr.IsArray = true;
            }
            if (tp.Value.IsOperator("[") && tp.Next().Value.IsOperator("]"))
            {
                tp = tp.Next();
                tp = tp.Next();
                tr.IsDoubleArray = true;
            }
            return(tr);
        }
Пример #4
0
        TsFunctionDecl ParseTsIndexer(ref TokenPointer tp)
        {
            var func = new TsFunctionDecl {
                IsIndexer = true
            };

            tp = ParseBracketParametersAndReturnType(tp, func, "[", "]");
            return(func);
        }
Пример #5
0
        TokenPointer ParseBracketParametersAndReturnType(TokenPointer tp, TsFunctionDecl func, string openBracket, string closeBracket)
        {
            tp = ParseBracketParameters(tp, func, openBracket, closeBracket);

            if (tp.Value.IsOperator(":"))
            {
                tp        = tp.Next();
                func.Type = ParseTypeRef(ref tp);
            }
            tp = tp.Verify(t => t.IsOperator(";")).Next();
            return(tp);
        }
Пример #6
0
        TokenPointer ParseBracketParameters(TokenPointer tp, TsFunctionDecl func, string openBracket, string closeBracket)
        {
            tp = tp.Verify(t => t.IsOperator(openBracket)).Next();

            if (!tp.Value.IsOperator(closeBracket))
            {
                while (true)
                {
                    var prm = ParseTsParameter(ref tp);
                    func.Parameters.Add(prm);
                    if (!tp.Value.IsOperator(","))
                    {
                        break;
                    }
                    tp = tp.Next();
                }
            }
            tp = tp.Verify(t => t.IsOperator(closeBracket)).Next();
            return(tp);
        }