| Author | Dave Jarvis <email> |
|---|---|
| Date | 2021-06-15 08:15:21 GMT-0700 |
| Commit | 9d7091ebd809570bfe934b8ccce109b1004fdf8b |
| Parent | a98944d |
| * | ||
| * @param consumer Receives emitted {@link Token}s. | ||
| + * @return The list of lexemes that could not be resolved. | ||
| */ | ||
| - public void parse( final Consumer<Token> consumer ) { | ||
| + public List<Lexeme> parse( final Consumer<Token> consumer ) { | ||
| final var lexemes = new CircularFifoQueue<Lexeme>( 3 ); | ||
| resolve( unresolved, consumer ); | ||
| - System.out.println( "UNRESOLVED: " + unresolved.size() ); | ||
| + final var result = new ArrayList<Lexeme>( unresolved.size() ); | ||
| + | ||
| + unresolved.forEach( ( lex ) -> result.add( lex[ 1 ] ) ); | ||
| + | ||
| + return result; | ||
| } | ||
| import java.util.ArrayList; | ||
| import java.util.Map; | ||
| +import java.util.function.Consumer; | ||
| import static com.keenwrite.quotes.TokenType.*; | ||
| import static java.util.Collections.sort; | ||
| /** | ||
| * Responsible for replacing {@link Token} instances with equivalent smart | ||
| - * quotes (or straight quotes). | ||
| + * quotes (or straight quotes). This will inform the caller when ambiguous | ||
| + * quotes cannot be reliably resolved. | ||
| */ | ||
| public class SmartQuotes { | ||
| ); | ||
| - public static String replace( final String text ) { | ||
| + /** | ||
| + * Converts | ||
| + * @param text | ||
| + * @return | ||
| + */ | ||
| + public static String convert( final String text ) { | ||
| + return convert( text, ( lexeme ) -> {} ); | ||
| + } | ||
| + | ||
| + public static String convert( | ||
| + final String text, final Consumer<Lexeme> consumer ) { | ||
| final var parser = new Parser( text ); | ||
| final var tokens = new ArrayList<Token>(); | ||
| package com.keenwrite.quotes; | ||
| +import org.junit.jupiter.api.Disabled; | ||
| import org.junit.jupiter.api.Test; | ||
| */ | ||
| public class SmartQuotesTest { | ||
| + /** | ||
| + * This is a single-use test that is useful for debugging. | ||
| + */ | ||
| @Test | ||
| + @Disabled | ||
| public void test_parse_SingleLine_Parsed() { | ||
| - out.println( SmartQuotes.replace( | ||
| - "What's this '-5.5'',' '-10.2'' cm,' and another '-7.25''' thing?" | ||
| + out.println( SmartQuotes.convert( | ||
| + "What's this '-5.5'',' '-10.2'' cm,' and another '-7.25''' thing?", | ||
| + out::println | ||
| ) ); | ||
| } | ||
| + /** | ||
| + * Tests that straight quotes are converted to curly quotes. | ||
| + * | ||
| + * @throws IOException Error opening file full of fun. | ||
| + */ | ||
| @Test | ||
| public void test_Parse_StraightQuotes_CurlyQuotes() throws IOException { | ||
| - testParser( SmartQuotes::replace ); | ||
| + testConverter( ); | ||
| } | ||
| - public void testParser( final Function<String, String> parser ) | ||
| + /** | ||
| + * Reads a file full of couplets. The first of the pair is the input, | ||
| + * the second of the pair is the expected result. Couplets may include | ||
| + * newline characters to indicate end of lines and end of paragraphs. | ||
| + * Lines that start with {@code #} are ignored. | ||
| + * | ||
| + * @param parser The text processor capable of straight quote conversion. | ||
| + * @throws IOException Error opening file full of fun. | ||
| + */ | ||
| + private void testConverter( final Function<String, String> parser ) | ||
| throws IOException { | ||
| try( final var reader = openResource( "smartypants.txt" ) ) { | ||
| Delta | 45 lines added, 8 lines removed, 37-line increase |
|---|