Beispiel #1
0
        TsTypeDecl ParseClassOrInterface(ref TokenPointer tp)
        {
            var iface = new TsTypeDecl {
                Token = tp,
            };

            if (tp.Value.IsIdentifier("interface"))
            {
                iface.Kind = TsTypeKind.Interface;
            }
            else if (tp.Value.IsIdentifier("class"))
            {
                iface.Kind = TsTypeKind.Class;
            }
            else
            {
                throw new Exception();
            }
            tp = tp.Next().Verify(t => t.IsIdentifier());
            var typeName = tp;

            iface.Name = typeName.Value.Text;
            tp         = tp.Next();
            if (tp.Value.IsIdentifier("extends"))
            {
                iface.Extends.AddRange(ParseExtends(ref tp));
            }
            if (tp.Value.IsIdentifier("implements"))
            {
                tp = tp.Next();
                iface.Implements.AddRange(ParseImplements(ref tp));
            }
            tp = ParseBracesAndMembers(tp, iface);
            return(iface);
        }
Beispiel #2
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);
     }
 }
Beispiel #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);
 }
Beispiel #4
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();
     }
 }