Пример #1
0
        TsParameter ParseTsParameter(ref TokenPointer tp)
        {
            var isParams = false;

            if (tp.Value.Is(TokenTypes.ArgAny)) //...
            {
                isParams = true;
                tp       = tp.Next();
            }
            tp = tp.Verify(t => t.IsIdentifier());

            var          varName  = tp;
            TokenPointer optional = null;

            tp = tp.Next();
            if (tp.Value.IsOperator("?"))
            {
                optional = tp;
                tp       = tp.Next();
            }
            var node = new TsParameter {
                Token = varName, Name = varName.Value.Text, IsOptional = optional != null, IsParams = isParams
            };

            if (!tp.Value.IsOperator(":"))
            {
                return(node); //allow Echo(s);
            }
            tp = tp.Verify(t => t.IsOperator(":")).Next();

            node.Type = ParseTypeRef(ref tp);
            //tp = tp.Next();
            return(node);
        }
Пример #2
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);
        }
Пример #3
0
 TokenPointer ParseBracesAndMembers(TokenPointer tp, TsTypeDecl iface)
 {
     tp = tp.Verify(t => t.IsOperator("{")).Next();
     while (!tp.Value.IsOperator("}"))
     {
         tp = SkipComments(tp);
         var me = ParseTsMember(ref tp);
         iface.Members.Add(me);
     }
     tp = tp.Verify(t => t.IsOperator("}")).Next();
     return(tp);
 }
Пример #4
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));
     }
 }
Пример #5
0
 TsMemberDecl ParseVar(ref TokenPointer tp)
 {
     tp = tp.Next();
     if (tp.Next(2).Value.IsOperator("{"))
     {
         var iface = new TsTypeDecl {
             Token = tp, Name = tp.Value.Text, Kind = TsTypeKind.Anonymous
         };
         tp = tp.Next();
         tp = tp.Next();
         tp = ParseBracesAndMembers(tp, iface);
         if (tp.Value != null && tp.Value.IsOperator(";"))
         {
             tp = tp.Next();
         }
         return(iface);
     }
     else
     {
         var prm = ParseTsParameter(ref tp);
         tp = tp.Verify(t => t.IsOperator(";")).Next();
         var decl = new TsVarDecl {
             Token = prm.Token, Type = prm.Type, Name = prm.Name
         };
         return(decl);
     }
 }
Пример #6
0
        TsFieldDecl ParseTsField(ref TokenPointer tp)
        {
            var prm = ParseTsParameter(ref tp);

            tp = tp.Verify(t => t.IsOperator(";")).Next();
            return(new TsFieldDecl {
                Token = prm.Token, Type = prm.Type, Name = prm.Name, IsOptional = prm.IsOptional
            });
        }
Пример #7
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);
        }
Пример #8
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);
        }
Пример #9
0
 TsNode ParseDeclare(ref TokenPointer tp)
 {
     tp = tp.Verify(t => t.IsIdentifier("declare")).Next();
     if (tp.Value.IsIdentifier("var"))
     {
         return(ParseVar(ref tp));
     }
     else if (tp.Value.IsIdentifier("function"))
     {
         tp = tp.Next();
         var func = ParseTsFunction(ref tp);
         return(func);
     }
     else if (tp.Value.IsIdentifier("module"))
     {
         var module = new TsTypeDecl {
             Token = tp, Kind = TsTypeKind.Module
         };
         tp = tp.Next();
         if (tp.Value.Is(TokenTypes.StringLiteral))
         {
             module.Name = tp.Value.Text;
         }
         else if (tp.Value.IsIdentifier())
         {
             module.Name = tp.Value.Text;
         }
         else
         {
             throw new Exception();
         }
         tp = tp.Next().Verify(t => t.IsOperator("{")).Next();
         while (!tp.Value.IsOperator("}"))
         {
             tp = SkipComments(tp);
             if (tp.Value.IsIdentifier("import"))
             {
                 var import = new TsImportDecl {
                     Token = tp
                 };
                 tp          = tp.Next().Verify(t => t.IsIdentifier());
                 import.Name = tp.Value.Text;
                 tp          = tp.Next();
                 tp          = tp.Verify(t => t.IsOperator("=")).Next();
                 tp          = tp.Verify(t => t.IsIdentifier("module")).Next();
                 tp          = tp.Verify(t => t.IsOperator("(")).Next();
                 tp          = tp.Verify(t => t.Is(TokenTypes.StringLiteral));
                 module.Type = new TsModuleTypeRef {
                     Name = tp.Value.Text
                 };
                 tp = tp.Next().Verify(t => t.IsOperator(")")).Next();
                 tp = tp.Verify(t => t.IsOperator(";")).Next();
                 module.Members.Add(import);
             }
             else if (tp.Value.IsIdentifier("export"))
             {
                 tp = tp.Next();
                 var me = ParseModuleMember(ref tp);
                 module.Members.Add(me);
             }
             else
             {
                 var me = ParseModuleMember(ref tp);
                 module.Members.Add(me);
             }
         }
         tp = tp.Verify(t => t.IsOperator("}")).Next();
         return(module);
     }
     else
     {
         throw new Exception();
     }
 }