private static string GetFieldValue(IBuffer buffer, YamlLexer lexer, string name)
        {
            SkipWhitespaceAndNewLine(lexer);
            var currentToken = lexer.TokenType;

            if (currentToken == YamlTokenType.NS_PLAIN_ONE_LINE_IN)
            {
                var text = buffer.GetText(new TextRange(lexer.TokenStart, lexer.TokenEnd));
                if (!text.Equals(name))
                {
                    return(null);
                }
            }
            lexer.Advance();
            SkipWhitespaceAndNewLine(lexer);

            currentToken = lexer.TokenType;
            if (currentToken != YamlTokenType.COLON)
            {
                return(null);
            }

            lexer.Advance();
            SkipWhitespaceAndNewLine(lexer);

            currentToken = lexer.TokenType;
            if (currentToken == YamlTokenType.NS_PLAIN_ONE_LINE_IN)
            {
                var text = buffer.GetText(new TextRange(lexer.TokenStart, lexer.TokenEnd));
                lexer.Advance();
                return(text);
            }

            return(null);
        }
        public Parser(string fileName, string contents, ErrorContainer errors)
        {
            _fileName       = fileName;
            _errorContainer = errors;

            _yaml = new YamlLexer(new StringReader(contents), fileName);
        }
Exemple #3
0
        public static Dictionary <string, string> Read(TextReader reader, string filenameHint = null)
        {
            var properties = new Dictionary <string, string>(StringComparer.Ordinal);

            var yaml = new YamlLexer(reader, filenameHint);

            while (true)
            {
                var t = yaml.ReadNext();
                if (t.Kind == YamlTokenKind.EndOfFile)
                {
                    break;
                }

                if (t.Kind != YamlTokenKind.Property)
                {
                    // $$$ error
                    t = YamlToken.NewError(t.Span, "Only properties are supported in this context");
                }
                if (t.Kind == YamlTokenKind.Error)
                {
                    // ToString will include source span  and message.
                    throw new InvalidOperationException(t.ToString());
                }

                properties[t.Property] = t.Value;
            }
            return(properties);
        }
Exemple #4
0
        public static FileID GetFileId(IBuffer buffer, YamlLexer lexer)
        {
            SkipWhitespaceAndNewLine(lexer);
            if (lexer.TokenType == YamlTokenType.LBRACE)
            {
                lexer.Advance();
                return(GetFileIdInner(buffer, lexer));
            }

            return(null);
        }
Exemple #5
0
        [DataRow("Foo:\r\n  val\r\n")] // Must have escape if there's a newline
        public void ExpectedError2(string expr)
        {
            var sr = new StringReader(expr);
            var y  = new YamlLexer(sr);

            var tokenOk = y.ReadNext();

            Assert.AreNotEqual(YamlTokenKind.Error, tokenOk.Kind);

            AssertLexError(y);
        }
Exemple #6
0
 public static void SkipWhitespace(YamlLexer lexer)
 {
     while (true)
     {
         var tokenType = lexer.TokenType;
         if (tokenType == null || tokenType != YamlTokenType.WHITESPACE)
         {
             return;
         }
         lexer.Advance();
     }
 }
Exemple #7
0
 private static void SkipWhitespaceAndNewLine(YamlLexer lexer)
 {
     while (true)
     {
         var tokenType = lexer.TokenType;
         if (tokenType == null || tokenType != YamlTokenType.WHITESPACE && tokenType != YamlTokenType.NEW_LINE)
         {
             return;
         }
         lexer.Advance();
     }
 }
Exemple #8
0
        public void ReadBasic()
        {
            var text =
                @"P1: =123
P2: =456
";
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);

            AssertLex("P1=123", y);
            AssertLex("P2=456", y);
            AssertLexEndFile(y);
            AssertLexEndFile(y);
        }
Exemple #9
0
        public void ReadBasicMultiline()
        {
            var text =
                @"M1: |
    =abc
    def
P1: =123
";
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);

            AssertLex("M1=abc\r\ndef\r\n", y);
            AssertLex("P1=123", y);
            AssertLexEndFile(y);
        }
        private static void FillTargetAndMethod(IBuffer buffer, OneToSetMap <string, FileID> eventHandlerToScriptTarget)
        {
            var lexer = new YamlLexer(buffer, false, false);

            lexer.Start();


            TokenNodeType currentToken;

            FileID currentTarget = null;
            string currentMethod = null;

            while ((currentToken = lexer.TokenType) != null)
            {
                if (currentToken == YamlTokenType.NS_PLAIN_ONE_LINE_IN)
                {
                    var text = buffer.GetText(new TextRange(lexer.TokenStart, lexer.TokenEnd));
                    lexer.Advance();
                    SkipWhitespace(lexer);
                    currentToken = lexer.TokenType;
                    if (currentToken == YamlTokenType.COLON)
                    {
                        if (text.Equals("m_Target"))
                        {
                            if (currentMethod != null && currentTarget != null)
                            {
                                eventHandlerToScriptTarget.Add(currentMethod, currentTarget);
                            }

                            lexer.Advance();
                            currentTarget = GetFileId(buffer, lexer);
                        }
                        else if (text.Equals("m_MethodName"))
                        {
                            Assertion.Assert(currentTarget != null, "currentTarget != null");
                            lexer.Advance();
                            currentMethod = GetPrimitiveValue(buffer, lexer);
                        }
                    }
                }
                lexer.Advance();
            }

            if (currentMethod != null && currentTarget != null)
            {
                eventHandlerToScriptTarget.Add(currentMethod, currentTarget);
            }
        }
Exemple #11
0
 public static bool FindNextIndent(YamlLexer lexer)
 {
     while (true)
     {
         var tokenType = lexer.TokenType;
         if (tokenType == null)
         {
             return(false);
         }
         if (tokenType == YamlTokenType.INDENT)
         {
             return(true);
         }
         lexer.Advance();
     }
 }
Exemple #12
0
        public void ReadEmptyObjectsError()
        {
            var text =
                @"P0: =1
Obj1:
    Obj2:
 ErrorObj3:
";
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);

            AssertLex("P0=1", y);
            AssertLex("Obj1:", y);
            AssertLex("Obj2:", y);
            AssertLexError(y); // Obj3 is at a bad indent.
        }
Exemple #13
0
        public void ReadBasicMultiline2()
        {
            // subsequent line in multiline (def) doesn't start at the same indentation
            // as first line. This means there are leading spaces on the 2nd line.
            var text =
                @"M1: |
    =abc
      def
P1: =123
";
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);

            AssertLex("M1=abc\r\n  def\r\n", y);
            AssertLex("P1=123", y);
            AssertLexEndFile(y);
        }
Exemple #14
0
        public static string GetPrimitiveValue(IBuffer buffer, YamlLexer lexer)
        {
            SkipWhitespace(lexer);
            var token = lexer.TokenType;

            if (token == YamlTokenType.NS_PLAIN_ONE_LINE_IN || token == YamlTokenType.NS_PLAIN_ONE_LINE_OUT)
            {
                return(buffer.GetText(new TextRange(lexer.TokenStart, lexer.TokenEnd)));
            }

            if (token == YamlTokenType.NEW_LINE)
            {
                return(string.Empty);
            }

            return(null);
        }
Exemple #15
0
        public void ReadBasicSpans()
        {
            var text =
                @"Obj1:
   P1: =456

Obj2:
";
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr, "test.yaml");

            AssertLex("Obj1:", y, "test.yaml:1,1-1,6");
            AssertLex("P1=456", y, "test.yaml:2,4-2,12");
            AssertLexEndObj(y);
            AssertLex("Obj2:", y, "test.yaml:4,1-4,6");
            AssertLexEndObj(y);
            AssertLexEndFile(y);
        }
Exemple #16
0
        private static YamlToken[] ReadAllTokens(string text)
        {
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);

            List <YamlToken> tokens = new List <YamlToken>();
            YamlToken        token;

            do
            {
                token = y.ReadNext();
                tokens.Add(token);
                Assert.AreNotEqual(YamlTokenKind.Error, token.Kind);

                // Fragments are small. If we don't terminate, there's a parser bug.
                Assert.IsTrue(tokens.Count < 100, "fragment failed to parse to EOF");
            } while (token.Kind != YamlTokenKind.EndOfFile);
            return(tokens.ToArray());
        }
Exemple #17
0
        public static FileID GetFileId(IBuffer buffer)
        {
            var lexer = new YamlLexer(buffer, false, false);

            lexer.Start();

            if (lexer.TokenType == YamlTokenType.INDENT)
            {
                lexer.Advance();
            }

            SkipWhitespaceAndNewLine(lexer);
            if (lexer.TokenType == YamlTokenType.LBRACE)
            {
                lexer.Advance();
                return(GetFileIdInner(buffer, lexer));
            }

            return(null);
        }
Exemple #18
0
        public static FileID GetFileIdInner(IBuffer buffer, YamlLexer lexer)
        {
            var fileId = GetFieldValue(buffer, lexer, "fileID");

            if (fileId == null)
            {
                return(null);
            }

            SkipWhitespace(lexer);
            if (lexer.TokenType != YamlTokenType.COMMA)
            {
                return(new FileID(null, fileId));
            }
            lexer.Advance();

            var guid = GetFieldValue(buffer, lexer, "guid");

            return(new FileID(guid, fileId));
        }
Exemple #19
0
        public void ShouldLoad()
        {
            // execute
            var tree = YamlLexer.LoadResourcesFromString(EXAMPLE_FILE);

            // Assert
            Assert.Equal("canadacentral", tree.Location);
            Assert.Equal(3, tree.Resources.Count());
            Assert.Single(tree.Resources, resource =>
                          resource.ResourceType == "resourceGroup" &&
                          resource.StringProperties["name"] == "defaultrg" &&
                          resource.StringProperties["value"] == "$NAME");
            Assert.Single(tree.Resources, resource =>
                          resource.ResourceType == "storageAccount" &&
                          resource.StringProperties["resourceGroup"] == "defaultrg" &&
                          resource.ListProperties["containers"].Length == 1 &&
                          resource.ListProperties["containers"][0] == "testData");
            Assert.Single(tree.Resources, resource =>
                          resource.ResourceType == "serviceBus" &&
                          resource.StringProperties["name"] == "the value");
        }
Exemple #20
0
        public static IHierarchyReference GetReferenceBySearcher(IPsiSourceFile assetSourceFile, IBuffer assetDocumentBuffer, StringSearcher searcher)
        {
            var start = searcher.Find(assetDocumentBuffer, 0, assetDocumentBuffer.Length);

            if (start < 0)
            {
                return(null);
            }
            var end = ourBracketSearcher.Find(assetDocumentBuffer, start, assetDocumentBuffer.Length);

            if (end < 0)
            {
                return(null);
            }

            var buffer   = ProjectedBuffer.Create(assetDocumentBuffer, new TextRange(start, end + 1));
            var lexer    = new YamlLexer(buffer, false, false);
            var parser   = new YamlParser(lexer.ToCachingLexer());
            var document = parser.ParseDocument();

            return((document.Body.BlockNode as IBlockMappingNode)?.Entries.FirstOrDefault()?.Content.Value.ToHierarchyReference(assetSourceFile));
        }
Exemple #21
0
        public void ErrorOnDuplicate()
        {
            var text =
                @"P1: =123
Obj1:
  P1: =Nested object, not duplicate
p1: =Casing Different, not duplicate
P2: =456
P1: =duplicate!
";
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);

            AssertLex("P1=123", y);
            AssertLex("Obj1:", y);
            AssertLex("P1=Nested object, not duplicate", y);
            AssertLexEndObj(y);
            AssertLex("p1=Casing Different, not duplicate", y);
            AssertLex("P2=456", y);

            AssertLexError(y);
        }
Exemple #22
0
        public void ReadEmptyObjects()
        {
            var text =
                @"P0: =1
Obj1:
    Obj2:
        Obj3:
Obj4:
";
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);

            AssertLex("P0=1", y);
            AssertLex("Obj1:", y);
            AssertLex("Obj2:", y);
            AssertLex("Obj3:", y);
            AssertLexEndObj(y); // Obj3
            AssertLexEndObj(y); // Obj2
            AssertLexEndObj(y); // Obj1
            AssertLex("Obj4:", y);
            AssertLexEndObj(y); // Obj4
            AssertLexEndFile(y);
        }
Exemple #23
0
        public void ReadComments()
        {
            var text =
                @"Obj1:
   # this starts on line 2, column 4
  P1: =123
# comment2
";
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);

            AssertLex("Obj1:", y);
            AssertLex("P1=123", y);
            AssertLexEndObj(y); // Obj1
            AssertLexEndFile(y);

            // Comments in yaml get stripped, but we get a warning and source pointer.
            Assert.IsNotNull(y._commentStrippedWarning);
            var loc = y._commentStrippedWarning.Value;

            Assert.AreEqual(2, loc.StartLine);
            Assert.AreEqual(4, loc.StartChar);
        }
        private void OpenChameleon()
        {
            Assertion.Assert(!myOpened, "!myOpened");
            AssertSingleChild();

            var service = Language.LanguageService();

            Assertion.Assert(service != null, "service != null");

            var buffer    = GetTextAsBuffer();
            var baseLexer = new YamlLexer(buffer, true, true)
            {
                currentLineIndent = myLexerIndent
            };
            var lexer           = new TokenBuffer(baseLexer).CreateLexer();
            var parser          = (YamlParser)service.CreateParser(lexer, null, GetSourceFile());
            var openedChameleon = parser.ParseContent(myParserIndent, myExpectedIndent);

            AssertTextLength(openedChameleon);

            DeleteChildRange(firstChild, lastChild);
            OpenChameleonFrom(openedChameleon);
        }
Exemple #25
0
        public void ReadObject()
        {
            var text =
                @"P0: =123
Obj1:
  P1a: =ABC
  Obj2:
    P2a: =X
    P2b: =Y
    P2c: =Z
  'Obj3:':
    P3a: =X
  ""'Ob\""j4'"":
    P4a: =X
  P1b: =DEF
";
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);

            AssertLex("P0=123", y);
            AssertLex("Obj1:", y);
            AssertLex("P1a=ABC", y);
            AssertLex("Obj2:", y);
            AssertLex("P2a=X", y);
            AssertLex("P2b=Y", y);
            AssertLex("P2c=Z", y);
            AssertLexEndObj(y);         // Obj2
            AssertLex("'Obj3:':", y);
            AssertLex("P3a=X", y);
            AssertLexEndObj(y);         // Obj3
            AssertLex("'Ob\"j4':", y);
            AssertLex("P4a=X", y);
            AssertLexEndObj(y); // Obj4
            AssertLex("P1b=DEF", y);
            AssertLexEndObj(y); // Obj1
            AssertLexEndFile(y);
        }
Exemple #26
0
        public void ReadClosing()
        {
            var text =
                @"P0: =1
Obj1:
  Obj2:
    P1: =1

    P2: =2
P3: =3
";
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);

            AssertLex("P0=1", y);
            AssertLex("Obj1:", y);
            AssertLex("Obj2:", y);
            AssertLex("P1=1", y);
            AssertLex("P2=2", y); // the newline prior isn't a token.
            AssertLexEndObj(y);   // Obj2
            AssertLexEndObj(y);   // Obj1
            AssertLex("P3=3", y);
            AssertLexEndFile(y);
        }
Exemple #27
0
 static void AssertLexEndObj(YamlLexer y)
 {
     AssertLex("<EndObj>", y);
 }
Exemple #28
0
        static void AssertLexError(YamlLexer y)
        {
            var p = y.ReadNext();

            Assert.AreEqual(YamlTokenKind.Error, p.Kind);
        }
Exemple #29
0
        static void AssertLex(string expected, YamlLexer y)
        {
            var p = y.ReadNext();

            Assert.AreEqual(expected, p.ToString());
        }
Exemple #30
0
        public static void ExtractSimpleAndReferenceValues(IBuffer buffer, Dictionary <string, string> simpleValues, Dictionary <string, FileID> referenceValues)
        {
            // special field for accessing anchor id
            simpleValues["&anchor"] = GetAnchorFromBuffer(buffer);

            var lexer = new YamlLexer(buffer, true, false);

            lexer.Start();

            TokenNodeType currentToken;
            bool          noColon = true;

            while ((currentToken = lexer.TokenType) != null)
            {
                if (noColon)
                {
                    if (currentToken == YamlTokenType.COLON)
                    {
                        noColon = false;
                    }

                    if (currentToken == YamlTokenType.NS_PLAIN_ONE_LINE_OUT)
                    {
                        var key = buffer.GetText(new TextRange(lexer.TokenStart, lexer.TokenEnd));

                        // special filed for checking stripped documents
                        if (key.Equals("stripped"))
                        {
                            simpleValues["stripped"] = "1";
                        }
                    }
                }

                if (currentToken == YamlTokenType.INDENT)
                {
                    lexer.Advance();
                    currentToken = lexer.TokenType;
                    if (currentToken == YamlTokenType.NS_PLAIN_ONE_LINE_IN)
                    {
                        var key = buffer.GetText(new TextRange(lexer.TokenStart, lexer.TokenEnd));

                        lexer.Advance();
                        SkipWhitespace(lexer);

                        currentToken = lexer.TokenType;
                        if (currentToken == YamlTokenType.COLON)
                        {
                            lexer.Advance();
                            SkipWhitespace(lexer);

                            currentToken = lexer.TokenType;
                            if (currentToken == YamlTokenType.LBRACE)
                            {
                                lexer.Advance();
                                var result = GetFileIdInner(buffer, lexer);
                                if (result != null)
                                {
                                    referenceValues[key] = result;
                                }
                            }
                            else if (YamlTokenType.CHAMELEON_BLOCK_MAPPING_ENTRY_CONTENT_WITH_ANY_INDENT.Equals(currentToken))
                            {
                                // sometimes, FileId is multiline
                                var result = GetFileId(ProjectedBuffer.Create(buffer, new TextRange(lexer.TokenStart, lexer.TokenEnd)));
                                if (result != null)
                                {
                                    referenceValues[key] = result;
                                }
                            }
                            else
                            {
                                var result = GetPrimitiveValue(buffer, lexer);
                                if (result != null)
                                {
                                    simpleValues[key] = result;
                                }
                            }
                        }
                    }
                    else
                    {
                        FindNextIndent(lexer);
                    }
                }
                else
                {
                    lexer.Advance();
                }
            }
        }