diff options
Diffstat (limited to 'src')
6 files changed, 40 insertions, 27 deletions
diff --git a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/DocumentMatcherParser.java b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/DocumentMatcherParser.java index 368bc81..418b4be 100644 --- a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/DocumentMatcherParser.java +++ b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/DocumentMatcherParser.java @@ -12,8 +12,8 @@ import java.util.function.BiConsumer; public class DocumentMatcherParser { public static BiConsumer<StyleMapBuilder, HtmlPath> parse(TokenIterator tokens) { - String identifier = tokens.nextValue(TokenType.IDENTIFIER); - switch (identifier) { + Token identifier = tokens.next(TokenType.IDENTIFIER); + switch (identifier.getValue()) { case "p": ParagraphMatcher paragraph = parseParagraphMatcher(tokens); return (builder, path) -> builder.mapParagraph(paragraph, path); @@ -29,7 +29,7 @@ public class DocumentMatcherParser { case "strike": return StyleMapBuilder::strikethrough; default: - throw new ParseException("Unrecognised document element: " + identifier); + throw new ParseException(identifier, "Unrecognised document element: " + identifier); } } @@ -82,13 +82,14 @@ public class DocumentMatcherParser { } private static boolean parseListType(TokenIterator tokens) { - String listType = tokens.nextValue(TokenType.IDENTIFIER); - if (listType.equals("ordered-list")) { - return true; - } else if (listType.equals("unordered-list")) { - return false; - } else { - throw new ParseException("Unrecognised list type: " + listType); + Token listType = tokens.next(TokenType.IDENTIFIER); + switch (listType.getValue()) { + case "ordered-list": + return true; + case "unordered-list": + return false; + default: + throw new ParseException(listType, "Unrecognised list type: " + listType); } } } diff --git a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/ParseException.java b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/ParseException.java index 718bb30..61a7c22 100644 --- a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/ParseException.java +++ b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/ParseException.java @@ -1,7 +1,7 @@ package org.zwobble.mammoth.internal.styles.parsing; public class ParseException extends RuntimeException { - public ParseException(String message) { - super(message); + public ParseException(Token token, String message) { + super("error reading style map at character " + (token.getCharacterIndex() + 1) + ": " + message); } } diff --git a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/StyleMappingTokeniser.java b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/StyleMappingTokeniser.java index 4c6a2a9..9806d67 100644 --- a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/StyleMappingTokeniser.java +++ b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/StyleMappingTokeniser.java @@ -14,7 +14,7 @@ public class StyleMappingTokeniser { index += token.getValue().length(); } - tokens.add(new Token(TokenType.EOF, "")); + tokens.add(new Token(index, TokenType.EOF, "")); return tokens; } @@ -32,7 +32,7 @@ public class StyleMappingTokeniser { index -= 1; } } - return new Token(tokenType, line.substring(startIndex, index)); + return new Token(startIndex, tokenType, line.substring(startIndex, index)); } private static TokenType tokenType(char nextChar) { diff --git a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/Token.java b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/Token.java index f6b771e..3059e5b 100644 --- a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/Token.java +++ b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/Token.java @@ -1,14 +1,20 @@ package org.zwobble.mammoth.internal.styles.parsing; public class Token { + private final int characterIndex; private final TokenType tokenType; private final String value; - public Token(TokenType tokenType, String value) { + public Token(int characterIndex, TokenType tokenType, String value) { + this.characterIndex = characterIndex; this.tokenType = tokenType; this.value = value; } + public int getCharacterIndex() { + return characterIndex; + } + public TokenType getTokenType() { return tokenType; } diff --git a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/TokenIterator.java b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/TokenIterator.java index f006007..7ee27e9 100644 --- a/src/main/java/org/zwobble/mammoth/internal/styles/parsing/TokenIterator.java +++ b/src/main/java/org/zwobble/mammoth/internal/styles/parsing/TokenIterator.java @@ -19,39 +19,45 @@ public class TokenIterator { return tokens.get(index + count).getTokenType(); } - public String nextValue(TokenType type) { + public Token next(TokenType type) { Token token = tokens.get(index); if (token.getTokenType() == type) { index += 1; - return token.getValue(); + return token; } else { - throw unexpectedTokenType(type, token.getTokenType()); + throw unexpectedTokenType(type, token); } } + public String nextValue(TokenType type) { + return next(type).getValue(); + } + public void skip() { index += 1; } public void skip(TokenType tokenType) { - if (peekTokenType() != tokenType) { - throw unexpectedTokenType(tokenType, peekTokenType()); + Token token = tokens.get(index); + if (token.getTokenType() != tokenType) { + throw unexpectedTokenType(tokenType, token); } index += 1; } public void skip(TokenType tokenType, String tokenValue) { - if (peekTokenType() != tokenType) { - throw unexpectedTokenType(tokenType, peekTokenType()); + Token token = tokens.get(index); + if (token.getTokenType() != tokenType) { + throw unexpectedTokenType(tokenType, token); } - String actualValue = tokens.get(index).getValue(); + String actualValue = token.getValue(); if (!actualValue.equals(tokenValue)) { - throw new ParseException("expected " + tokenType + " token with value " + tokenValue + " but value was " + actualValue); + throw new ParseException(token, "expected " + tokenType + " token with value " + tokenValue + " but value was " + actualValue); } index += 1; } - private ParseException unexpectedTokenType(TokenType expected, TokenType actual) { - return new ParseException("expected token of type " + expected + " but was of type " + actual); + private ParseException unexpectedTokenType(TokenType expected, Token actual) { + return new ParseException(actual, "expected token of type " + expected + " but was of type " + actual.getTokenType()); } } diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index 4f4c9fd..1f50fc5 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -198,7 +198,7 @@ public class MammothTests { () -> new DocumentConverter().addStyleMap("p =>\np[style-name=] =>")); assertThat( exception.getMessage(), - equalTo("expected token of type STRING but was of type CLOSE_SQUARE_BRACKET")); + equalTo("error reading style map at character 14: expected token of type STRING but was of type CLOSE_SQUARE_BRACKET")); } @Test |
