Skip to content
Browse files

Fix handling of newlines

  • Loading branch information...
1 parent 38fbcd4 commit 28c123486cc4508dc9308b66f9961355c875b866 Nikolai Weibull committed May 22, 2012
Showing with 124 additions and 16 deletions.
  1. +34 −16 src/parser.c
  2. +90 −0 test/testsuite.at
View
50 src/parser.c
@@ -48,25 +48,42 @@ substring(struct nmc_parser *parser, YYSTYPE *value, const xmlChar *end, int typ
return token(parser, end, type);
}
+static int
+dedent(struct nmc_parser *parser, const xmlChar *end)
+{
+ /* TODO: assert(parser->dedents > 0); */
+ parser->dedents--;
+ return token(parser, end, DEDENT);
+}
+
+static int
+dedents(struct nmc_parser *parser, const xmlChar *end, int spaces)
+{
+ parser->dedents = (parser->indent - spaces) / 2;
+ parser->indent -= 2 * parser->dedents;
+ /* TODO: assert(parser->indent >= 0); */
+ return dedent(parser, end);
+}
+
int
nmc_parser_lex(struct nmc_parser *parser, YYSTYPE *value)
{
const xmlChar *end = parser->p;
-dedents:
- if (parser->dedents > 0) {
- parser->dedents--;
- return token(parser, end, DEDENT);
- }
+ if (parser->dedents > 0)
+ return dedent(parser, end);
+bol:
if (parser->bol) {
+ /* TODO Make this into a function */
parser->bol = false;
if (xmlStrncmp(end, BAD_CAST " ", 2) == 0)
return token(parser, end + 2, PARAGRAPH);
else if (xmlStrncmp(end, BAD_CAST "§ ", xmlUTF8Size(BAD_CAST "§ ")) == 0)
return token(parser, end + xmlUTF8Size(BAD_CAST "§ "), SECTION);
else if (xmlStrncmp(end, BAD_CAST "", xmlUTF8Size(BAD_CAST "")) == 0)
return token(parser, end + xmlUTF8Size(BAD_CAST ""), ENUMERATION);
+ */
else if (*end == '\0')
return END;
else
@@ -97,23 +114,24 @@ nmc_parser_lex(struct nmc_parser *parser, YYSTYPE *value)
return token(parser, parser->p + parser->indent, INDENT);
} else if (spaces < parser->indent && spaces % 2 == 0) {
parser->want = ERROR;
- parser->dedents = (parser->indent - spaces) / 2;
- parser->indent -= 2 * parser->dedents;
- goto dedents;
+ return dedents(parser, end, spaces);
} else {
parser->want = ERROR;
return token(parser, parser->p, BLOCKSEPARATOR);
}
} else {
- if (end != parser->p)
+ int spaces = end - parser->p;
+ if (spaces > parser->indent) {
return token(parser, end, CONTINUATION);
- else if (parser->indent > 0) {
- /* TODO: Not quite sure about this test. We
- * might want to check if end == '\0' &&
- * parser->indent > 0 instead. */
- parser->dedents = parser->indent / 2;
- parser->indent = 0;
- goto dedents;
+ } else if (spaces < parser->indent) {
+ if (spaces % 2 != 0)
+ return token(parser, end, CONTINUATION);
+ parser->bol = true;
+ return dedents(parser, end, spaces);
+ } else {
+ parser->bol = true;
+ parser->p = end;
+ goto bol;
}
}
}
View
90 test/testsuite.at
@@ -102,6 +102,48 @@ AT_NMC_CHECK([title],
AT_CLEANUP
+AT_SETUP([Document consisting of a title followed by a section containing two paragraphs])
+
+AT_DATA([title],
+[[Title
+
+§ Section
+
+ Paragraph1
+
+ Paragraph2
+]])
+
+AT_NMC_CHECK([title],
+[[ <title>Title</title>
+ <section>
+ <title>Section</title>
+ <p>Paragraph1</p>
+ <p>Paragraph2</p>
+ </section>]])
+
+AT_CLEANUP
+
+AT_SETUP([Document consisting of a title followed by a section containing a multi-line paragraph])
+
+AT_DATA([title],
+[[Title
+
+§ Section
+
+ Line1
+ Line2
+]])
+
+AT_NMC_CHECK([title],
+[[ <title>Title</title>
+ <section>
+ <title>Section</title>
+ <p>Line1 Line2</p>
+ </section>]])
+
+AT_CLEANUP
+
AT_SETUP([Document consisting of a title followed by two sections])
AT_DATA([title],
@@ -152,3 +194,51 @@ AT_NMC_CHECK([title],
<p>Paragraph2</p>]])
AT_CLEANUP
+
+AT_SETUP([Document consisting of a title followed by a two-item enumeration])
+
+AT_DATA([title],
+[[Title
+
+• Item1
+• Item2
+]])
+
+AT_NMC_CHECK([title],
+[[ <title>Title</title>
+ <enumeration>
+ <item>
+ <p>Item1</p>
+ </item>
+ <item>
+ <p>Item2</p>
+ </item>
+ </enumeration>]])
+
+AT_CLEANUP
+
+AT_SETUP([Document consisting of a title followed by a two-item enumeration, the first containing two paragraphs])
+
+AT_DATA([title],
+[[Title
+
+• Line1
+ Line2
+
+ Paragraph2
+• Item2
+]])
+
+AT_NMC_CHECK([title],
+[[ <title>Title</title>
+ <enumeration>
+ <item>
+ <p>Line1 Line2</p>
+ <p>Paragraph2</p>
+ </item>
+ <item>
+ <p>Item2</p>
+ </item>
+ </enumeration>]])
+
+AT_CLEANUP

0 comments on commit 28c1234

Please sign in to comment.
Something went wrong with that request. Please try again.