-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from chedim/feature/priority-sorting
Feature/priority sorting
- Loading branch information
Showing
23 changed files
with
1,041 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 54 additions & 15 deletions
69
src/main/java/com/onkiup/linker/parser/ParserLocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,79 @@ | ||
package com.onkiup.linker.parser; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ParserLocation { | ||
private final String sourceName; | ||
private final String source; | ||
private final int line, row; | ||
|
||
public ParserLocation(String sourceName, String source, int line, int row) { | ||
this.sourceName = sourceName; | ||
this.source = source; | ||
private static final Logger logger = LoggerFactory.getLogger(ParserLocation.class); | ||
public static ParserLocation ZERO = new ParserLocation("unknown", 0,0,0); | ||
|
||
private final int line, column, position; | ||
private final String name; | ||
|
||
public ParserLocation(String name, int position, int line, int column) { | ||
if (position < 0) { | ||
throw new IllegalArgumentException("Position cannot be negative"); | ||
} | ||
|
||
if (line < 0) { | ||
throw new IllegalArgumentException("Line cannot be negative"); | ||
} | ||
|
||
if (column < 0) { | ||
throw new IllegalArgumentException("Column cannot be negative"); | ||
} | ||
|
||
this.name = name; | ||
this.position = position; | ||
this.line = line; | ||
this.row = row; | ||
this.column = column; | ||
} | ||
|
||
public String sourceName() { | ||
return sourceName; | ||
public String name() { | ||
return name; | ||
} | ||
|
||
public String source() { | ||
return source; | ||
public int position() { | ||
return position; | ||
} | ||
|
||
public int line() { | ||
return line; | ||
} | ||
|
||
public int row() { | ||
return row; | ||
public int column() { | ||
return column; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringBuilder().append(sourceName == null ? "" : sourceName + ':') | ||
return new StringBuilder() | ||
.append(name) | ||
.append(" - ") | ||
.append(line) | ||
.append(':') | ||
.append(row) | ||
.append(column) | ||
.toString(); | ||
} | ||
|
||
|
||
public ParserLocation advance(CharSequence source) { | ||
int position = this.position + source.length(); | ||
int line = this.line; | ||
int column = this.column; | ||
for (int i = 0; i < source.length(); i++) { | ||
if (source.charAt(i) == '\n') { | ||
line++; | ||
column = 0; | ||
} else { | ||
column++; | ||
} | ||
} | ||
|
||
ParserLocation result = new ParserLocation(name, position, line, column); | ||
logger.debug("Advanced from {} to {} using chars: '{}'", this, result, source); | ||
return result; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,46 @@ | ||
package com.onkiup.linker.parser; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Arrays; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import com.onkiup.linker.parser.token.PartialToken; | ||
import com.onkiup.linker.parser.token.RuleToken; | ||
import com.onkiup.linker.parser.token.VariantToken; | ||
|
||
public class SyntaxError extends RuntimeException { | ||
public SyntaxError(String message) { | ||
super(message); | ||
|
||
private PartialToken lastToken; | ||
private StringBuilder source; | ||
private String message; | ||
|
||
public SyntaxError(String message, PartialToken lastToken, StringBuilder source) { | ||
this.message = message; | ||
this.lastToken = lastToken; | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
PartialToken expected = lastToken.expected(); | ||
StringBuilder result = new StringBuilder("Parser error:") | ||
.append(message) | ||
.append("\n") | ||
.append("\tExpected ") | ||
.append(expected) | ||
.append(" but got: '") | ||
.append(expected != null && source != null && expected.position() < source.length() ? source.substring(expected.position()) : source) | ||
.append("'\n\tSource:\n\t\t") | ||
.append(source) | ||
.append("\n\n\tTraceback:\n\t\t"); | ||
|
||
PartialToken parent = expected; | ||
while (null != (parent = (PartialToken) parent.getParent().orElse(null))) { | ||
result.append(parent.toString().replace("\n", "\n\t\t")); | ||
} | ||
|
||
return result.toString(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.