Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenquotes.git

Match double primes

AuthorDave Jarvis <email>
Date2021-06-01 21:30:57 GMT-0700
Commit100fc760579caadf026caeef0e93ec5619cb4d3e
Parent2a21d6d
lib/src/main/java/com/keenwrite/quotes/Parser.java
}
+ for( final var lex : mLexemes ) {
+ parse( lex, consumer );
+ }
+
// Create/convert a list of all unambiguous quotations.
// Let TERM ::= (, | ; | ! | ? | .)
if( lex2.isType( QUOTE_SINGLE ) && lex3.isType( WORD ) &&
lex1.anyType( WORD, PERIOD, NUMBER ) ) {
+ // Examples: y'all, Ph.D.'ll, 20's
consumer.accept( new Token( QUOTE_APOSTROPHE, lex2 ) );
flush( mLexemes );
}
else if( lex1.isType( QUOTE_SINGLE ) && lex3.isType( QUOTE_SINGLE ) &&
"n".equalsIgnoreCase( lex2.toString( mText ) ) ) {
+ // I.e., 'n'
consumer.accept( new Token( QUOTE_APOSTROPHE, lex1 ) );
consumer.accept( new Token( QUOTE_APOSTROPHE, lex3 ) );
}
else if( lex1.isType( NUMBER ) && lex2.isType( QUOTE_SINGLE ) ) {
- consumer.accept( new Token( QUOTE_PRIME_SINGLE, lex2 ) );
+ if( lex3.isType( QUOTE_SINGLE ) ) {
+ // E.g., 2''
+ consumer.accept(
+ new Token( QUOTE_PRIME_DOUBLE, lex2.began(), lex3.ended() ) );
+ }
+ else {
+ // E.g., 2'
+ consumer.accept( new Token( QUOTE_PRIME_SINGLE, lex2 ) );
+ }
}
else if( lex1.isType( NUMBER ) && lex2.isType( QUOTE_DOUBLE ) ) {
+ // E.g., 2"
consumer.accept( new Token( QUOTE_PRIME_DOUBLE, lex2 ) );
}
else if( lex1.isType( QUOTE_SINGLE ) && lex2.isType( WORD ) &&
+ // E.g., 'contraction
beginsUnambiguously( lex2.toString( mText ) ) ) {
consumer.accept( new Token( QUOTE_APOSTROPHE, lex1 ) );
}
else if( lex1.isType( ESC_SINGLE ) ) {
+ // E.g., \'
consumer.accept( new Token( QUOTE_STRAIGHT_SINGLE, lex1 ) );
}
else if( lex1.isType( ESC_DOUBLE ) ) {
- consumer.accept( new Token( QUOTE_STRAIGHT_DOUBLE, lex2 ) );
+ // E.g., \"
+ consumer.accept( new Token( QUOTE_STRAIGHT_DOUBLE, lex1 ) );
}
else if( lex1.anyType( QUOTE_SINGLE, QUOTE_DOUBLE ) ) {
lib/src/main/java/com/keenwrite/quotes/Token.java
public class Token implements Comparable<Token> {
private final TokenType mType;
- private final Lexeme mLexeme;
+ final int mBegan;
+ final int mEnded;
- public Token( final TokenType type, final Lexeme lexeme ) {
+ /**
+ * Convenience constructor to create a token that uses the lexeme's offsets.
+ *
+ * @param type The type of {@link Token} to create.
+ * @param lexeme Container for beginning and ending text offsets.
+ */
+ Token( final TokenType type, final Lexeme lexeme ) {
+ this( type, lexeme.began(), lexeme.ended() );
+ }
+
+ Token( final TokenType type, final int began, final int ended ) {
assert type != null;
- assert lexeme != null;
+ assert began >= 0;
+ assert ended > began;
mType = type;
- mLexeme = lexeme;
+ mBegan = began;
+ mEnded = ended;
}
TokenType getType() {
return mType;
}
int began() {
- return mLexeme.began();
+ return mBegan;
}
int ended() {
- return mLexeme.ended();
+ return mEnded;
}
@Override
public int compareTo( final Token that ) {
- return mLexeme.compareTo( that.mLexeme );
- }
-
- public String toString( final String text ) {
- return mLexeme.toString( text );
+ return this.mBegan - that.mBegan;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"mType=" + mType +
- ", mLexeme=" + mLexeme +
+ ", mBegan=" + mBegan +
+ ", mEnded=" + mEnded +
'}';
}
lib/src/test/java/com/keenwrite/quotes/LexerTest.java
/**
- * Tests lexing words, numbers, punctuation, spaces, and newlines.
+ * Tests lexing words, numbers, punctuation, spaces, newlines, etc.
*/
class LexerTest {
lib/src/test/resources/com/keenwrite/quotes/smartypants.txt
+# ########################################################################
+# Escaped quotes
+# ########################################################################
+She stood 5\'7\".
+She stood 5'7".
+
+# ########################################################################
+# Primes (single, double)
+# ########################################################################
+
+# No space after the feet sign.
+It's 4'11" away.
+It&apos;s 4&prime;11&Prime; away.
+
+Alice's friend is 6'3" tall.
+Alice&apos;s friend is 6&prime;3&Prime; tall.
+
+Bob's table is 5'' × 4''.
+Bob&apos;s table is 5&Prime; × 4&Prime;.
+
+Bob's table is 5'×4'.
+Bob&apos;s table is 5&prime;×4&prime;.
+
+What's this -5.5'' all about?
+What&apos;s this -5.5&Prime; all about?
+
++7.9'' is weird.
++7.9&Prime; is weird.
+
+Use 11.5"x14.25" virtual paper!
+Use 11.5&Prime;x14.25&Prime; virtual paper!
+
+Angular measure 3° 5' 30" means 3 degs, 5 arcmins, & 30 arcsecs.
+Angular measure 3° 5&prime; 30&Prime; means 3 degs, 5 arcmins, & 30 arcsecs.
+
+# ########################################################################
+# Inside contractions (no leading/trailing apostrophes)
+# ########################################################################
+I don't like it: I love's it!
+I don&apos;t like it: I love&apos;s it!
+
+We'd've thought that pancakes'll be sweeter there.
+We&apos;d&apos;ve thought that pancakes&apos;ll be sweeter there.
+
+She'd be coming o'er when the horse'd gone to pasture...
+She&apos;d be coming o&apos;er when the horse&apos;d gone to pasture...
+
+# ########################################################################
+# Beginning contractions (leading apostrophes)
+# ########################################################################
+'Twas and 'tis whate'er lay 'twixt dawn and dusk 'n River Styx.
+&apos;Twas and &apos;tis whate&apos;er lay &apos;twixt dawn and dusk &apos;n River Styx.
+
+# ########################################################################
+# Outside contractions (leading and trailing, no middle)
+# ########################################################################
+Salt 'n' vinegar, fish-'n'-chips, sugar 'n' spice!
+Salt &apos;n&apos; vinegar, fish-&apos;n&apos;-chips, sugar &apos;n&apos; spice!
+
+# ########################################################################
+# Ending contractions (trailing apostrophes)
+# ########################################################################
+Didn' get th' message.
+Didn&apos; get th&apos; message.
+
+Namsayin', y'know what I'ma sayin'?
+Namsayin&apos;, y&apos;know what I&apos;ma sayin&apos;?
+
+# ########################################################################
+# Decades
+# ########################################################################
+The Roaring '20s had the best music, no?
+The Roaring &apos;20s had the best music, no?
+
+Took place in '04, yes'm!
+Took place in &apos;04, yes&apos;m!
+
+# ########################################################################
+# Backticks (left and right double quotes)
+# ########################################################################
+``I am Sam''
+&ldquo;I am Sam&rdquo;
+
+``Sam's away today''
+&ldquo;Sam&apos;s away today&rdquo;
+
+``Sam's gone!
+&ldquo;Sam&apos;s gone!
+
+``5'10" tall 'e was!''
+&ldquo;5&prime;10&Prime; tall &apos;e was!&rdquo;
+
+# ########################################################################
+# Consecutive quotes
+# ########################################################################
+"'I'm trouble.'"
+&ldquo;&lsquo;I&apos;m trouble.&rsquo;&rdquo;
+
+'"Trouble's my name."'
+&lsquo;&ldquo;Trouble&apos;s my name.&ldquo;&lsquo;
+
+# ########################################################################
+# Escaped quotes
+# ########################################################################
+\"What?\"
+&ldquo;What?&rdquo;
+
+# ########################################################################
+# Double quotes
+# ########################################################################
+"I am Sam"
+&ldquo;I am Sam&rdquo;
+
+"...even better!"
+&ldquo;...even better!&rdquo;
+
+"It was so," said he.
+&ldquo;It was so,&rdquo; said he.
+
+"She said, 'Llamas'll languish, they'll--
+&ldquo;She said, &lsquo;Llamas&apos;ll languish, they&apos;ll--
+
+With "air quotes" in the middle.
+With &ldquo;air quotes&rdquo; in the middle.
+
+With--"air quotes"--and dashes.
+With--&ldquo;air quotes&rdquo;--and dashes.
+
+"Not "quite" what you expected?"
+&ldquo;Not &ldquo;quite&rdquo; what you expected?&rdquo;
+
+# ########################################################################
+# Nested quotations
+# ########################################################################
+"'Here I am,' said Sam"
+&ldquo;&lsquo;Here I am,&rsquo; said Sam&rdquo;
+
+'"Here I am," said Sam'
+&lsquo;&ldquo;Here I am,&rdquo;, said Sam&rsquo;
+
+'Hello, "Dr. Brown," what's your real name?'
+&lsquo;Hello, &ldquo;Dr. Brown,&rdquo; what's your real name?&rsquo;
+
+"'Twas, t'wasn't thy name, 'twas it?" said Jim "the Barber" Brown.
+&ldquo;&apos;Twas, t&apos;wasn&apos;t thy name, &apos;twas it?&rdquo; said Jim &ldquo;the Barber&rdquo; Brown.
+
+# ########################################################################
+# Single quotes
+# ########################################################################
+'I am Sam'
+&lsquo;I am Sam&rsquo;
+
+'It was so,' said he.
+&lsquo;It was so,&rsquo; said he.
+
+'...even better!'
+&lsquo;...even better!&rsquo;
+
+With 'quotes' in the middle.
+With &lsquo;quotes&rsquo; in the middle.
+
+With--'imaginary'--dashes.
+With--&lsquo;imaginary&rsquo;--dashes.
+
+'Not 'quite' what you expected?'
+&lsquo;Not &lsquo;quite&rsquo; what you expected?&rsquo;
+
+''Cause I don't like it, 's why,' said Pat.
+&lsquo;&apos;Cause I don't like it, &apos;s why,&rsquo; said Pat.
+
+'It's a beautiful day!'
+&lsquo;It&apos;s a beautiful day!&rsquo;
+
+'He said, "Thinkin'."'
+&lsquo;He said, &ldquo;Thinkin&rsquo;.&rdquo;&rsquo;
+
+# ########################################################################
+# Possessives
+# ########################################################################
+Sam's Sams' and the Ross's roses' thorns were prickly.
+Sam&apos;s Sams&apos; and the Ross&apos;s roses&apos; thorns were prickly.
+
+# ########################################################################
+# Mixed
+# ########################################################################
+"I heard she said, 'That's Sam's'," said the Sams' cat.
+&ldquo;I heard she said, &lsquo;That&apos;s Sam&apos;s&rsquo;,&rdquo; said the Sams&apos; cat.
+
+"'Janes' said, ''E'll be spooky, Sam's son with the jack-o'-lantern!'" said the O'Mally twins'---y'know---ghosts in unison.
+&ldquo;&lsquo;Janes&apos; said, &lsquo;&apos;E&apos;ll be spooky, Sam&apos;s son with the jack-o&apos;-lantern!&rsquo;&rdquo; said the O&apos;Mally twins&apos;---y&apos;know---ghosts in unison.
+
+'He's at Sams'
+&lsquo;He&apos; at Sams&rsquo;
+
+\"Hello!\"
+&ldquo;Hello!&rdquo;
+
+ma'am
+ma&apos;am
+
+'Twas midnight
+&apos;Twas midnight
+
+\"Hello,\" said the spider. \"'Shelob' is my name.\"
+&ldquo;Hello,&rdquo; said the spider. &ldquo;&lsquo;Shelob&rsquo; is my name.&rdquo;
+
+'A', 'B', and 'C' are letters.
+&lsquo;A&rsquo; &lsquo;B&rsquo; and &lsquo;C&rsquo; are letters.
+
+'Oak,' 'elm,' and 'beech' are names of trees. So is 'pine.'
+&lsquo;Oak,&rsquo; &lsquo;elm,&rsquo; and &lsquo;beech&rsquo; are names of trees. So is &lsquo;pine.&rsquo;
+
+'He said, \"I want to go.\"' Were you alive in the 70's?
+&lsquo;He said, &ldquo;I want to go.&rdquo;&rsquo; Were you alive in the 70&apos;s?
+
+\"That's a 'magic' sock.\"
+&ldquo;That&apos;s a &lsquo;magic&rsquo; sock.&rdquo;
+
+Website! Company Name, Inc. (\"Company Name\" or \"Company\") recommends reading the following terms and conditions, carefully:
+Website! Company Name, Inc. (&ldquo;Company Name&rdquo; or &ldquo;Company&rdquo;) recommends reading the following terms and conditions, carefully:
+
+Website! Company Name, Inc. ('Company Name' or 'Company') recommends reading the following terms and conditions, carefully:
+Website! Company Name, Inc. (&lsquo;Company Name&rsquo; or &lsquo;Company&rsquo;) recommends reading the following terms and conditions, carefully:
+
+Workin' hard
+Workin&apos; hard
+
+'70s are my favorite numbers,' she said.
+&lsquo;70s are my favorite numbers,&rsquo; she said.
+
+'70s fashion was weird.
+&apos;70s fashion was weird.
+
+12\" record, 5'10\" height
+12&Prime; record, 5&prime;10&Prime; height
+
+Model \"T2000\"
+Model &ldquo;T2000&rdquo;
+
+iPad 3's battery life is not great.
+iPad 3&apos;s battery life is not great.
+
+Book 'em, Danno. Rock 'n' roll. 'Cause 'twas the season.
+Book &apos;em, Danno. Rock &apos;n&apos; roll. &apos;Cause &apos;twas the season.
+
+'85 was a good year. (The entire '80s were.)
+&apos;85 was a good year. (The entire &apos;80s were.)
Delta290 lines added, 15 lines removed, 275-line increase