Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jun 18, 2020
2 parents bfd0156 + dd1aac9 commit 20c6625
Show file tree
Hide file tree
Showing 24 changed files with 565 additions and 83 deletions.
31 changes: 23 additions & 8 deletions src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import com.amihaiemil.eoyaml.exceptions.YamlReadingException;
import java.util.Collection;
import java.util.Iterator;

/**
* YamlLines default implementation. "All" refers to the fact that
Expand Down Expand Up @@ -72,11 +73,14 @@ public Collection<YamlLine> original() {
}

@Override
public YamlNode toYamlNode(final YamlLine prev) {
public YamlNode toYamlNode(
final YamlLine prev,
final boolean guessIndentation
) {
final YamlNode node;
final String prevLine = prev.trimmed();
if(prevLine.isEmpty()) {
node = this.mappingSequenceOrPlainScalar(prev);
node = this.mappingSequenceOrPlainScalar(prev, guessIndentation);
} else {
final String lastChar = prevLine.substring(prevLine.length() - 1);

Expand All @@ -85,21 +89,33 @@ public YamlNode toYamlNode(final YamlLine prev) {
} else if (lastChar.equals(Follows.FOLDED_BLOCK_SCALAR)) {
node = new ReadFoldedBlockScalar(prev, this);
} else if (prevLine.matches(Follows.FOLDED_SEQUENCE)) {
node = new ReadYamlSequence(prev, this);
node = new ReadYamlSequence(prev, this, guessIndentation);
} else {
node = this.mappingSequenceOrPlainScalar(prev);
node = this.mappingSequenceOrPlainScalar(
prev, guessIndentation
);
}
}
return node;
}

@Override
public Iterator<YamlLine> iterator() {
return this.lines.iterator();
}

/**
* Try to figure out what YAML node (mapping, sequence or scalar) is found
* after the given line.
* @param prev YamlLine just previous to the node we're trying to find.
* @param guessIndentation If true, we will guess the correct indentation,
* if any YAML line is misplaced.
* @return Found YamlNode.
*/
private YamlNode mappingSequenceOrPlainScalar(final YamlLine prev) {
private YamlNode mappingSequenceOrPlainScalar(
final YamlLine prev,
final boolean guessIndentation
) {
final YamlNode node;
final YamlLine first = new Skip(
this,
Expand All @@ -111,13 +127,12 @@ private YamlNode mappingSequenceOrPlainScalar(final YamlLine prev) {
line -> line.trimmed().startsWith("!!")
).iterator().next();
if (first.trimmed().startsWith("-")){
node = new ReadYamlSequence(prev, this);
node = new ReadYamlSequence(prev, this, guessIndentation);
} else if(first.trimmed().contains(":")) {
node = new ReadYamlMapping(prev, this);
node = new ReadYamlMapping(prev, this, guessIndentation);
} else if(this.original().size() == 1) {
node = new ReadPlainScalar(this, first);
} else {
System.out.println("LINE IS: [" + first.trimmed() + "]");
throw new YamlReadingException(
"Could not parse YAML starting at line " + (first.number() + 1)
+ " . It should be a sequence (line should start with '-'), "
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/amihaiemil/eoyaml/Backwards.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ public Collection<YamlLine> original() {
}

@Override
public YamlNode toYamlNode(final YamlLine prev) {
return this.lines.toYamlNode(prev);
public YamlNode toYamlNode(
final YamlLine prev,
final boolean guessIndentation
) {
return this.lines.toYamlNode(prev, guessIndentation);
}

@Override
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/amihaiemil/eoyaml/FirstCommentFound.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ public Collection<YamlLine> original() {
}

@Override
public YamlNode toYamlNode(final YamlLine prev) {
return this.lines.toYamlNode(prev);
public YamlNode toYamlNode(
final YamlLine prev,
final boolean guessIndentation
) {
return this.lines.toYamlNode(prev, guessIndentation);
}
}
7 changes: 5 additions & 2 deletions src/main/java/com/amihaiemil/eoyaml/GreaterIndentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public Collection<YamlLine> original() {
}

@Override
public YamlNode toYamlNode(final YamlLine prev) {
return this.yamlLines.toYamlNode(prev);
public YamlNode toYamlNode(
final YamlLine prev,
final boolean guessIndentation
) {
return this.yamlLines.toYamlNode(prev, guessIndentation);
}
}
88 changes: 88 additions & 0 deletions src/main/java/com/amihaiemil/eoyaml/Indented.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright (c) 2016-2020, Mihai Emil Andronache
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package com.amihaiemil.eoyaml;

/**
* An YAML Line indented by us. We override the line's
* initial indentation with a given value.
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 5.1.0
*/
final class Indented implements YamlLine {

/**
* Original YAML line.
*/
private final YamlLine original;

/**
* Given indentation.
*/
private int indentation;

/**
* Ctor.
* @param original Original YamlLine.
* @param indentation Given indentation.
*/
Indented(final YamlLine original, final int indentation) {
this.original = original;
this.indentation = indentation;
}

@Override
public String trimmed() {
return this.original.trimmed();
}

@Override
public String comment() {
return this.original.comment();
}

@Override
public int number() {
return this.original.number();
}

@Override
public int indentation() {
return this.indentation;
}

@Override
public boolean requireNestedIndentation() {
return this.original.requireNestedIndentation();
}

@Override
public int compareTo(final YamlLine other) {
return this.original.compareTo(other);
}
}
56 changes: 50 additions & 6 deletions src/main/java/com/amihaiemil/eoyaml/ReadYamlMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,31 @@ final class ReadYamlMapping extends BaseYamlMapping {
*/
private final YamlLines significant;

/**
* If set to true we will try to guess the correct indentation
* of misplaced lines.
*/
private final boolean guessIndentation;

/**
* Ctor.
* @param lines Given lines.
*/
ReadYamlMapping(final AllYamlLines lines) {
this(new YamlLine.NullYamlLine(), lines);
this(lines, Boolean.FALSE);
}

/**
* Ctor.
* @param lines Given lines.
* @param guessIndentation If true, we will try to guess the correct
* indentation of misplaced lines.
*/
ReadYamlMapping(
final AllYamlLines lines,
final boolean guessIndentation
) {
this(new YamlLine.NullYamlLine(), lines, guessIndentation);
}

/**
Expand All @@ -82,6 +101,21 @@ final class ReadYamlMapping extends BaseYamlMapping {
* @param lines Given lines.
*/
ReadYamlMapping(final YamlLine previous, final AllYamlLines lines) {
this(previous, lines, Boolean.FALSE);
}

/**
* Ctor.
* @param previous Line just before the start of this mapping.
* @param lines Given lines.
* @param guessIndentation If true, we will try to guess the correct
* indentation of misplaced lines.
*/
ReadYamlMapping(
final YamlLine previous,
final AllYamlLines lines,
final boolean guessIndentation
) {
this.previous = previous;
this.all = lines;
this.significant = new SameIndentationLevel(
Expand All @@ -94,9 +128,11 @@ final class ReadYamlMapping extends BaseYamlMapping {
line -> line.trimmed().startsWith("..."),
line -> line.trimmed().startsWith("%"),
line -> line.trimmed().startsWith("!!")
)
),
guessIndentation
)
);
this.guessIndentation = guessIndentation;
}

@Override
Expand All @@ -111,7 +147,9 @@ public Set<YamlNode> keys() {
) {
continue;
} else if ("?".equals(trimmed)) {
keys.add(this.significant.toYamlNode(line));
keys.add(
this.significant.toYamlNode(line, this.guessIndentation)
);
} else {
if(!trimmed.contains(":")) {
continue;
Expand Down Expand Up @@ -201,7 +239,9 @@ private YamlNode valueOfStringKey(final String key) {
|| trimmed.matches("^" + tryKey + "\\:[ ]*\\>$")
|| trimmed.matches("^" + tryKey + "\\:[ ]*\\|$")
) {
value = this.significant.toYamlNode(line);
value = this.significant.toYamlNode(
line, this.guessIndentation
);
} else if((trimmed.startsWith(tryKey + ":")
|| trimmed.startsWith("- " + tryKey + ":"))
&& trimmed.length() > 1
Expand Down Expand Up @@ -229,14 +269,18 @@ private YamlNode valueOfNodeKey(final YamlNode key) {
final YamlLine line = linesIt.next();
final String trimmed = line.trimmed();
if("?".equals(trimmed)) {
final YamlNode keyNode = this.significant.toYamlNode(line);
final YamlNode keyNode = this.significant.toYamlNode(
line, this.guessIndentation
);
if(keyNode.equals(key)) {
final YamlLine colonLine = linesIt.next();
if(":".equals(colonLine.trimmed())
|| colonLine.trimmed().matches("^\\:[ ]*\\>$")
|| colonLine.trimmed().matches("^\\:[ ]*\\|$")
) {
value = this.significant.toYamlNode(colonLine);
value = this.significant.toYamlNode(
colonLine, this.guessIndentation
);
} else if(colonLine.trimmed().startsWith(":")
&& (colonLine.trimmed().length() > 1)
){
Expand Down
Loading

0 comments on commit 20c6625

Please sign in to comment.