Exemple #1
0
        public void TestToString()
        {
            var expected = "10";
            var actual   = new StringLocation(10).ToString();

            Assert.AreEqual(expected, actual);
        }
Exemple #2
0
        public void TestParams()
        {
            var expected = 10;
            var actual   = new StringLocation(10).Position;

            Assert.AreEqual(expected, actual);
        }
Exemple #3
0
        public static void Generate(string scriptFileName, string basePath, ref string content, Action <SourceMapBuilder> beforeGenerate, Func <string, string> sourceContent, string[] names, IList <string> sourceFiles, UnicodeNewline?forceEols, ILogger logger)
        {
            var            fileName  = Path.GetFileName(scriptFileName);
            var            generator = new SourceMapGenerator(fileName, "", basePath, forceEols);
            StringLocation location  = null;
            string         script    = content;
            int            offset    = 0;

            content = tokenRegex.Replace(content, match =>
            {
                location       = SourceMapGenerator.LocationFromPos(script, match.Index, location, ref offset);
                offset        += match.Length;
                var sourceLine = int.Parse(match.Groups[2].Value);
                var sourceCol  = int.Parse(match.Groups[3].Value);
                var sourcePath = sourceFiles[int.Parse(match.Groups[1].Value)];
                //sourcePath = sourcePath.Substring(basePath.Length + 1);

                generator.RecordLocation(location.Line, location.Column, sourcePath, sourceLine, sourceCol);
                return("");
            });

            var           sources  = generator.SourceMapBuilder.SourceUrlList;
            List <string> contents = new List <string>();

            foreach (var source in sources)
            {
                contents.Add(sourceContent(source));
            }

            // Chrome handles it very strange, need more investigate
            //generator._sourceMapBuilder.SourceNameList.AddRange(names);

            beforeGenerate?.Invoke(generator.SourceMapBuilder);

            var map = generator.GetSourceMap(contents.ToArray());

            if (logger != null)
            {
                logger.Trace("SourceMap for " + scriptFileName);
                logger.Trace(map);
            }

            var encoded = "//# sourceMappingURL=data:application/json;base64,"
                          + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(map));

            if (logger != null)
            {
                logger.Trace("Base64 SourceMap for " + scriptFileName);
                logger.Trace(encoded);
            }

            content = content + Emitter.NEW_LINE + encoded + Emitter.NEW_LINE;
        }
Exemple #4
0
        public StringSelection TryParseComment(StringLocation loc)
        {
            var commentStart = loc.Select().ExtendIfAfterEqualsTo("/*");

            if (commentStart.IsEmpty)
            {
                return(commentStart);
            }
            var comment = commentStart.ExtendUntilIncluding("*/");

            return(comment);
        }
Exemple #5
0
        public StringSelection TryParseStringLiteral(StringLocation loc)
        {
            var s = loc.Select().ExtendIfAfterEqualsTo("\"");

            if (s.IsEmpty)
            {
                return(s);
            }
            s = s.ExtendCharsUntil(ch => ch == '\"');
            s = s.ExtendBy(1);
            return(s);
        }
Exemple #6
0
        public StringSelection TryParseComment2(StringLocation loc)
        {
            var commentStart = loc.Select().ExtendIfAfterEqualsTo("//");

            if (commentStart.IsEmpty)
            {
                return(commentStart);
            }
            var comment = commentStart.ExtendCharsUntil(ch => "\r\n".Contains(ch));

            return(comment);
        }
Exemple #7
0
 static Token NextToken(StringLocation loc, TsTokenTypes tokenTypes)
 {
     foreach (var tt in tokenTypes.All)
     {
         var sel = tt.TryParse(loc);
         if (!sel.IsEmpty)
         {
             return(new Token(tt, sel));
         }
     }
     return(null);
 }
Exemple #8
0
        public StringSelection TryParseOperator(StringLocation loc)
        {
            var ch = loc.Select(1);

            if (!ch.IsValid)
            {
                return(loc.Select());
            }
            if (":?;()[],{}=.><".Contains(ch.Text))
            {
                return(ch);
            }
            return(loc.Select());
        }
        private static StringLocation LocationFromPos(string s, int pos, StringLocation lastLocation, ref int offset)
        {
            int res = lastLocation?.Line ?? 1;
            int startLinePosition = lastLocation?.StartLinePosition ?? 0;
            int i;

            for (i = lastLocation?.Position ?? 0; i <= pos - 1; i++)
            {
                if (s[i] == Emitter.NEW_LINE_CHAR)
                {
                    startLinePosition = i;
                    offset            = 0;
                    res++;
                }
            }

            return(new StringLocation(res, pos - startLinePosition - offset, i, startLinePosition));
        }
Exemple #10
0
        /// <summary>
        /// CleanseListOfItem() removes empty (or null) strings entries as well as ones which match the search criteria defined by
        /// pattern and the location on the string where theh pattern must occur, in order to find a valid match
        /// </summary>
        /// <param name="stringLocation"></param>
        /// <param name="pattern"></param>
        public void CleanseListOfItem(StringLocation stringLocation, string pattern)
        {
            int index = 0;

            while (index < Words.Count)
            {
                if ((stringLocation == StringLocation.WholeWordMatch && Words[index].IndexOf(pattern) > -1) ||
                    (stringLocation == StringLocation.StartsWith && Words[index].StartsWith(pattern)) ||
                    (stringLocation == StringLocation.EndsWith && Words[index].EndsWith(pattern)) ||
                    // What about StringLocation.Includes?
                    String.IsNullOrEmpty(Words[index]))
                {
                    Words.Remove(Words[index]);
                }
                else
                {
                    index++;
                }
            }
        }
Exemple #11
0
        public static List <Token> Tokenize(string code)
        {
            var tokenTypes = TsTokenTypes.TypeScript;
            var loc2       = new StringLocation(code, 0);


            var tokens = new List <Token>();

            while (!loc2.IsAtEnd)
            {
                var token = NextToken(loc2, tokenTypes);
                if (token == null)
                {
                    throw new Exception("Cannot parse: {" + loc2.Select(30).Text + "}");
                }
                tokens.Add(token);
                loc2 = token.Selection.End;
            }
            tokens = tokens.Where(t => t.IsNot(tokenTypes.Whitespace)).ToList();
            return(tokens);
        }
Exemple #12
0
 public StringReader(string source, int offset, StringLocation location)
 {
     Source   = source;
     Offset   = offset;
     Location = location;
 }
Exemple #13
0
 public StringSelection TryParseIdentifier(StringLocation loc)
 {
     return(loc.Select().ExtendCharsAsLongAs(ch => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$".Contains(ch)));
 }
Exemple #14
0
 public StringSelection TryParseWhitespace(StringLocation loc)
 {
     return(loc.Select().ExtendCharsAsLongAs(ch => "\r\n ".Contains(ch)));
 }
Exemple #15
0
 public StringSelection TryParseArgAny(StringLocation loc)
 {
     return(loc.Select().ExtendIfAfterEqualsTo("..."));
 }
Exemple #16
0
 public StringSelection TryParseLambdaOperator(StringLocation loc)
 {
     return(loc.Select().ExtendIfAfterEqualsTo("=>"));
 }