Skip to content

Commit

Permalink
Update the Scalariform library for tokenizing Scala submissions
Browse files Browse the repository at this point in the history
The new version of Scalariform is 0.2.10. It is the last release
from 2019. The library is no longer maintained.

The built package scalariform.jar now includes the Scalariform library
as well as the small CLI script ScalariformTokens.
The JAR package is built for Scala 2.13 and can not be run with older
Scala versions.

Add sbt config files for building the scalariform.jar package with sbt.
The command `sbt assembly` builds the JAR package with the dependencies.
The build output is written to the scalariform/target/scala-2.13
directory and it must be copied from there manually.

If there is a need to move on from the dead Scalariform library,
one alternative could be Scalafmt:
https://scalameta.org/scalafmt/
  • Loading branch information
markkuriekkinen committed Feb 15, 2022
1 parent 29da156 commit 9e035e0
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Makefile
!CMakeLists.txt
compile_commands.json

tokenizer/scalariform/target/
tokenizer/scalariform/project/target/
tokenizer/scalariform/project/project/target/

**/bin
*.a
*.swp
Expand Down
2 changes: 1 addition & 1 deletion tokenizer/scala.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def tokenize(source, config):
"""
try:
parsed = run(("scala", "-cp", "tokenizer/scalariform:tokenizer/scalariform/scalariform.jar", "ScalariformTokens"), source)
parsed = run(("scala", "-cp", "tokenizer/scalariform/scalariform.jar", "ScalariformTokens"), source)
lines = parsed.decode("utf-8").split("\n", 1)
return lines[0].strip(), index_string_to_list(lines[1].strip())
except Exception as e:
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed tokenizer/scalariform/ScalariformTokens$.class
Binary file not shown.
Binary file not shown.
Binary file removed tokenizer/scalariform/ScalariformTokens.class
Binary file not shown.
6 changes: 3 additions & 3 deletions tokenizer/scalariform/ScalariformTokens.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ object ScalariformTokens extends App {
Tokens.XML_UNPARSED -> '$',
Tokens.XML_PROCESSING_INSTRUCTION -> '$')

val source = io.Source.stdin.getLines.mkString("\n")
val tokens = ScalaLexer.tokenise(source, scalaVersion = ScalaVersions.DEFAULT_VERSION)
val source = io.Source.stdin.getLines().mkString("\n")
val tokens = ScalaLexer.tokenise(source, scalaVersion = ScalaVersions.Scala_2_11.toString)
val filtered = tokens.filter(t => !t.tokenType.isComment && !t.tokenType.isNewline && t.tokenType != Tokens.EOF)

val ids = filtered.map(t => typeMap.getOrElse(t.tokenType, '?'))
val chars = filtered.map(t => t.offset + "-" + t.lastCharacterOffset)
val chars = filtered.map(t => s"${t.offset}-${t.lastCharacterOffset}")

println(ids.mkString(""))
println(chars.mkString(","))
Expand Down
15 changes: 15 additions & 0 deletions tokenizer/scalariform/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "2.13.8"

scalacOptions ++= Seq("-deprecation", "-feature")

// Scalariform is the library for parsing Scala code (the student's submissions).
// The Scalariform project was stopped in 2019 and is no longer maintained.
// https://mvnrepository.com/artifact/org.scalariform/scalariform
libraryDependencies += "org.scalariform" %% "scalariform" % "0.2.10"

// sbt-assembly plugin for building a JAR package that contains the dependencies as well.
// But don't include the Scala standard library.
assembly / mainClass := Some("ScalariformTokens")
assemblyPackageScala / assembleArtifact := false

1 change: 1 addition & 0 deletions tokenizer/scalariform/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.6.2
1 change: 1 addition & 0 deletions tokenizer/scalariform/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.2.0")
Binary file modified tokenizer/scalariform/scalariform.jar
Binary file not shown.

0 comments on commit 9e035e0

Please sign in to comment.