private static YamlBlock parseDocument(Input input) { YamlBlock doc = new YamlBlock(); string indent = ""; string line = input.readLine(); input.unreadLine(line); MatchCollection m = RE_INDENT.Matches(line); if (m.Count > 1) { indent = m[1].Value; } parseIntoContainer(input, doc, indent); line = input.readLine(); if (!"...".Equals(line)) { // end of document marker NOT reached. throw new YamlException(input, "Document end marker not found (aka c-document-end). " + "Tip: end your document with '...' or check indentation levels."); } return(doc); }
/** A shortcut for {@code getMapping().getKey(key).getString()} for string literal values. */ public string GetKeyString(string key) { YamlBlock value = GetKey(key); if (value != null) { if (!value.isLiteral()) { throw new YamlException( string.Format("Key '{0}' is of type '{1}', not literal", key, 0)); } return(value.GetString()); } return(null); }
public static void DoTest() { string testText = @"# Project: MiniYamlParser -- test data file -- (c) 2012 ralfoide -- Apache License, Version 2.0. --- format: 1.0 description: A key/value set used to configure an app of mine. It contains a multi-line script. items: - name: intro link: http://www.example.com/test1 dpi: 320 text: 1: All inner space is preserved. Rest is trimmed. 3: Interested? landscape: | resize width 100% move image 50% 40% to screen 85% 5% portrait: | resize height 75% move image 50% 0% to screen 10% 5% - name: family link: http://www.example.com/test2 dpi: 160 text: 1: All your 2: Bases 3: make 4: your time 5: belong to us. - name: sleep landscape: | text-color #AAAAAA text 1 at 4% 68% size 12% - name: work portrait: | font Serif text-color #AAAAAA ... # end"; YamlBlock doc = MiniYamlParser.parse(testText); Debug.Log(doc); }
public YamlBlock SetKeyValue(string key, YamlBlock value) { if (literal != null) { throw new YamlException("Block of type 'literal' can't be converted to type 'mapping'"); } else if (sequence != null) { throw new YamlException("Block of type 'sequence' can't be converted to type 'mapping'"); } if (mapping == null) { mapping = new Dictionary <string, YamlBlock>(); } mapping.Add(key, value); return(this); }
// --- Sequence container public YamlBlock AppendToSequence(YamlBlock block) { if (literal != null) { throw new YamlException("Block of type 'literal' can't be converted to type 'sequence'"); } else if (mapping != null) { throw new YamlException("Block of type 'mapping' can't be converted to type 'sequence'"); } if (sequence == null) { sequence = new List <YamlBlock>(); } sequence.Add(block); return(this); }
private static void parseIntoContainer(Input input, YamlBlock block, string indent) { try { string line; while ((line = input.readLine()) != null) { if ("...".Equals(line)) { // end of document marker reached. input.unreadLine(line); return; } MatchCollection m = RE_SEQ_AND_KEY.Matches(line); if (m.Count == 0) { m = RE_SEQ_OR_KEY.Matches(line); } if (m.Count > 0) { if (m.Count == 4) { } string i2 = m[1].Value; if (i2.Length > indent.Length) { throw new YamlException(input, string.Format("Mismatched map indentation, expected %d but was %d'", indent.Length, i2.Length)); } else if (i2.Length < indent.Length) { input.unreadLine(line); return; } YamlBlock c = new YamlBlock(); bool parseLiteral = true; if ("-".Equals(m[2].Value)) { // group 2 is the - for a pure sequence item //block.(c); if (m[3].Value != null) { // This is a combo sequence item + new key:value *inside* the // new sequence. We simulate this by handling this as a new // sequence item and then change the line by removing // the - marker and recursively iterate to handle a key:value item. line = line.Substring(0, i2.Length) + ' ' + line.Substring(i2.Length + 1); input.unreadLine(line); parseLiteral = false; } } else if (m[3].Value != null) { // group 3 is the key for a key:value item block.SetKeyValue(m[3].Value, c); } else { // This case should not happen. throw new YamlException(input, "Internal error; unmatched syntax: " + line); } if (parseLiteral) { string value = m[4].Value.Trim(); if ("|".Equals(value)) { // Parse literal string. The multi-line literal stops when // we encounter a potential key:value or sequence item at the // same or outer scope level. StringBuilder sb = new StringBuilder(); while ((line = input.readLine()) != null) { if ("...".Equals(line)) { // end of document marker reached. input.unreadLine(line); break; } MatchCollection m2 = RE_SEQ_OR_KEY.Matches(line); if (m2.Count > 1 && m2[1].Value.Length <= indent.Length) { // potential key:value or sequence item found. input.unreadLine(line); break; } sb.Append(line).Append('\n'); } c.SetLiteral(sb.ToString()); } else if (value.Length > 0) { c.SetLiteral(value); } } if (!c.isLiteral() && !c.isMapping() && !c.isSequence()) { line = input.readLine(); input.unreadLine(line); MatchCollection m2 = RE_INDENT.Matches(line); if (m2.Count > 1) { i2 = m2[1].Value; if (i2.Length > indent.Length) { parseIntoContainer(input, c, i2); } } } } else { throw new YamlException(input, "'key:' or '- sequence' expected, found: " + line); } } } catch (YamlException e) { } }