Skip to content

Commit 181c079

Browse files
committed
added more precise syntax error handling for parsing the markdown
1 parent 56495a1 commit 181c079

File tree

10 files changed

+110
-228
lines changed

10 files changed

+110
-228
lines changed

conversion2025/out.json

Lines changed: 0 additions & 70 deletions
This file was deleted.

conversion2025/out2.json

Lines changed: 0 additions & 129 deletions
This file was deleted.

conversion2025/pandoc_testing.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

conversion2025/pandoc_testingv2.txt

Lines changed: 0 additions & 14 deletions
This file was deleted.

conversion2025/tools/validator/src/main/scala/Main.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ object Main extends IOApp {
2020
val host: Host = Host.fromString("localhost").getOrElse(Host.fromString("0.0.0.0").get)
2121
val port: Port = Port.fromInt(8080).getOrElse(Port.fromInt(80).get)
2222

23+
// runs the server
2324
def run(args: List[String]): IO[ExitCode] =
2425
EmberServerBuilder
2526
.default[IO]

conversion2025/tools/validator/src/main/scala/lexer.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ import parsley.token.{Lexer, Basic}
33
import parsley.token.descriptions.*
44
import parsley.errors.combinator.*
55

6-
6+
// Define the lexer with a custom lexical description
77
object lexer {
8+
9+
// Define the set of generic keywords
810
private val genericKeywords = Set(
911
"$$", "$"
1012
)
1113

14+
// Define the lexical description
1215
private val desc = LexicalDesc.plain.copy(
16+
// Define how identifiers and symbols are recognized
1317
nameDesc = NameDesc.plain.copy(
1418
identifierStart = Basic(x => x.isInstanceOf[Char] && x != '$'),
1519
identifierLetter = Basic(x => x.isInstanceOf[Char] && x != '$'),
1620
),
21+
22+
// Define how symbols are recognized
1723
symbolDesc = SymbolDesc.plain.copy(
1824
hardOperators = genericKeywords
1925
)
@@ -23,7 +29,9 @@ object lexer {
2329

2430
def fully[A](p: Parsley[A]): Parsley[A] = lexer.fully(p)
2531

32+
// Define the text parser that recognizes regular text
2633
val text: Parsley[String] = lexer.lexeme.names.identifier
2734

35+
// Define the symbol parser that recognizes symbols
2836
val implicits = lexer.lexeme.symbol.implicits
2937
}

conversion2025/tools/validator/src/main/scala/parser.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ object parser {
2020

2121
private lazy val blocks = many(block)
2222

23+
// Define the block parser that recognizes different types of blocks
2324
private lazy val block: Parsley[Block] =
24-
atomic(Display(display)) |
25-
atomic(Inline(inline)) |
25+
Display(display) |
26+
Inline(inline) |
2627
Text(lexer.text)
2728

28-
private lazy val display = "$$" ~> lexer.text <~ "$$"
29-
private lazy val inline = "$" ~> lexer.text <~ "$"
29+
// Define the display math parser that recognizes display math blocks
30+
private lazy val display = "$$".label("Opening display math delimiter") ~> lexer.text <~ "$$".label("Closing display math delimiter")
31+
32+
// Define the inline math parser that recognizes inline math blocks
33+
private lazy val inline = "$".label("Opening inline math delimiter") ~> lexer.text <~ "$".label("Closing inline math delimiter")
3034
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Block.*
2+
3+
// check that the inline math does not contain newlines
4+
def inline_check(math: String): Boolean = !math.contains('\n')
5+
6+
// validate the markdown content
7+
def validate_markdown(markdown: MarkDown): Boolean = {
8+
markdown.content.forall {
9+
case Inline(m) => inline_check(m)
10+
case _ => true
11+
}
12+
}
13+
14+
private val INLINE_DELIM = "$"
15+
private val DISPLAY_DELIM = "\n$$\n"
16+
17+
// Rebuild the markdown content from the parsed structure
18+
def rebuild_markdown(markdown: MarkDown): String = {
19+
markdown.content.map {
20+
case Text(s) => s
21+
case Inline(m) => s"${INLINE_DELIM}${m}${INLINE_DELIM}"
22+
case Display(m) => s"${DISPLAY_DELIM}${m.trim}${DISPLAY_DELIM}"
23+
}.mkString
24+
}

conversion2025/tools/validator/src/main/scala/syntax.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import parsley.generic
22

3+
// This file defines the lexer for parsing mathematical expressions in Markdown format.
34
case class MarkDown(content: List[Block])
45
object MarkDown extends generic.ParserBridge1[List[Block],MarkDown]
56

67
enum Block {
7-
case Text(s: String) // Regular text block
8-
case Inline(m: String) // Inline Math block
9-
case Display(m: String) // Display Math block
8+
case Text(content: String) // Regular text block
9+
case Inline(content: String) // Inline Math block
10+
case Display(content: String) // Display Math block
1011
}
1112

1213
object Block {

0 commit comments

Comments
 (0)