Skip to content

Commit 3bd6209

Browse files
committed
begun basic implementation of parser for content validation suing the parsely API with scala
1 parent 8fa81eb commit 3bd6209

File tree

14 files changed

+172
-74
lines changed

14 files changed

+172
-74
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,3 +701,9 @@ media_ans/
701701
dist-newstyle/
702702
dist/
703703
.stack-work/
704+
705+
706+
# Scala build artifacts
707+
.bsp/
708+
.scala-build/
709+
.metals/

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.watcherExclude": {
3+
"**/target": true
4+
}
5+
}

conversion2025/mathpix_to_llm_to_in2lambda_to_JSON.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@
974974
" print(json.dumps(extracted_dict, indent=2))\n",
975975
" print(\"Now validating the content...\")\n",
976976
"\n",
977-
" # return extracted_dict\n",
977+
" return extracted_dict\n",
978978
"\n",
979979
" content_validated_dict = content_texdown_check(extracted_dict)\n",
980980
" print(\"successfully validated the content.\")\n",

conversion2025/testing.ipynb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@
7272
"output = pypandoc.convert_text('# Hello', 'html', format='md')\n",
7373
"print(output)"
7474
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"id": "5",
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"import requests\n",
84+
"\n",
85+
"data = \"x^2 + y^2 = z^2\"\n",
86+
"resp = requests.post(\"http://localhost:8080/process\", data=data)\n",
87+
"\n",
88+
"if resp.ok:\n",
89+
" print(\"✅ Processed:\", resp.text)\n",
90+
"else:\n",
91+
" print(\"❌ Error:\", resp.status_code)"
92+
]
7593
}
7694
],
7795
"metadata": {

conversion2025/tools/validator/app/Main.hs

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name := "validator"
2+
3+
version := "0.1"
4+
5+
scalaVersion := "3.3.1"
6+
7+
libraryDependencies ++= Seq(
8+
"org.typelevel" %% "cats-effect" % "3.5.1",
9+
"org.http4s" %% "http4s-dsl" % "1.0.0-M39",
10+
"org.http4s" %% "http4s-ember-server" % "1.0.0-M39",
11+
"ch.qos.logback" % "logback-classic" % "1.4.11",
12+
"com.github.j-mie6" %% "parsley" % "5.0.0-M9",
13+
"org.scalameta" %% "munit" % "1.0.0-M11" % Test
14+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.11.3
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import cats.effect._
2+
import org.http4s._
3+
import org.http4s.dsl.io._
4+
import org.http4s.ember.server.EmberServerBuilder
5+
import org.http4s.server.Router
6+
import com.comcast.ip4s.{Host, Port}
7+
8+
object Main extends IOApp {
9+
10+
val mathRoutes = HttpRoutes.of[IO] {
11+
case req @ POST -> Root / "process" =>
12+
req.as[String].flatMap { input =>
13+
val cleaned = input.trim.reverse // Example logic
14+
Ok(s"Processed: $cleaned")
15+
}
16+
}
17+
18+
val httpApp = Router("/" -> mathRoutes).orNotFound
19+
20+
val host: Host = Host.fromString("localhost").getOrElse(Host.fromString("0.0.0.0").get)
21+
val port: Port = Port.fromInt(8080).getOrElse(Port.fromInt(80).get)
22+
23+
def run(args: List[String]): IO[ExitCode] =
24+
EmberServerBuilder
25+
.default[IO]
26+
.withHost(host)
27+
.withPort(port)
28+
.withHttpApp(httpApp)
29+
.build
30+
.use(_ => IO.never)
31+
.as(ExitCode.Success)
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import parsley.Parsley
2+
import parsley.token.{Lexer, Basic}
3+
import parsley.token.descriptions.*
4+
import parsley.errors.combinator.*
5+
6+
7+
object lexer {
8+
private val genericKeywords = Set(
9+
"$$", "$"
10+
)
11+
12+
private val desc = LexicalDesc.plain.copy(
13+
nameDesc = NameDesc.plain.copy(
14+
identifierStart = Basic(_.isInstanceOf[Char]),
15+
identifierLetter = Basic(_.isInstanceOf[Char]),
16+
),
17+
symbolDesc = SymbolDesc.plain.copy(
18+
hardOperators = genericKeywords,
19+
)
20+
)
21+
22+
private val lexer = Lexer(desc)
23+
24+
def fully[A](p: Parsley[A]): Parsley[A] = lexer.fully(p)
25+
26+
val text: Parsley[String] = lexer.lexeme.names.identifier
27+
28+
val implicits = lexer.lexeme.symbol.implicits
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import parsley.quick.*
2+
import parsley.syntax.zipped.*
3+
import parsley.expr.chain
4+
import parsley.expr.{precedence, Ops, InfixL}
5+
import parsley.errors.ErrorBuilder
6+
import parsley.errors.combinator.*
7+
8+
import lexer.{fully, text}
9+
import lexer.implicits.implicitSymbol
10+
11+
import Block.*
12+
13+
14+
object parser {
15+
def parse[Err: ErrorBuilder](input: String): Either[Err, MarkDown] = parser.parse(input).toEither
16+
17+
private lazy val parser = fully(markDown)
18+
19+
private lazy val markDown = MarkDown(blocks)
20+
21+
private lazy val blocks = many(block)
22+
23+
private lazy val block: Parsley[Block] =
24+
Display(display) |
25+
Inline(inline) |
26+
Text(lexer.text)
27+
28+
private lazy val display = "$$" ~> lexer.text <~ "$$"
29+
private lazy val inline = "$" ~> lexer.text <~ "$"
30+
}

0 commit comments

Comments
 (0)