Skip to content

Commit

Permalink
Pass a Parser to FrontEnd instances
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Jan 14, 2021
1 parent 118789b commit 2f2b049
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
29 changes: 13 additions & 16 deletions amm/repl/src/main/scala/ammonite/repl/AmmoniteFrontEnd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package ammonite.repl

import java.io.{InputStream, OutputStream, OutputStreamWriter}

import ammonite.compiler.Highlighter
import ammonite.repl.api.FrontEnd
import ammonite.terminal.filters._
import GUILikeFilters.SelectionFilter
import ammonite.terminal._
import fastparse.Parsed
import ammonite.util.{Colors, Res}
import ammonite.compiler.Parsers
case class AmmoniteFrontEnd(extraFilters: Filter = Filter.empty) extends FrontEnd{
import ammonite.compiler.iface.Parser
case class AmmoniteFrontEnd(
parser: Parser,
extraFilters: Filter = Filter.empty
) extends FrontEnd{

def width = FrontEndUtils.width
def height = FrontEndUtils.height
Expand All @@ -27,13 +29,9 @@ case class AmmoniteFrontEnd(extraFilters: Filter = Filter.empty) extends FrontEn
case None => Res.Exit(())
case Some(code) =>
addHistory(code)
fastparse.parse(code, Parsers.Splitter(_)) match{
case Parsed.Success(value, idx) =>
Res.Success((code, value.map(_._2)))
case f @ Parsed.Failure(_, index, extra) =>
Res.Failure(
Parsers.formatFastparseError("(console)", code, f)
)
parser.split(code, ignoreIncomplete = false).get match{
case Right(value) => Res.Success((code, value))
case Left(error) => Res.Failure(error)
}
}
}
Expand All @@ -56,7 +54,7 @@ case class AmmoniteFrontEnd(extraFilters: Filter = Filter.empty) extends FrontEn
}
val details2 = for (d <- details) yield {

Parsers.defaultHighlight(
parser.defaultHighlight(
d.toVector,
colors.comment(),
colors.`type`(),
Expand Down Expand Up @@ -101,7 +99,7 @@ case class AmmoniteFrontEnd(extraFilters: Filter = Filter.empty) extends FrontEn
// Enter
val multilineFilter = Filter.action(
SpecialKeys.NewLine,
ti => Parsers.split(ti.ts.buffer.mkString).isEmpty
ti => parser.split(ti.ts.buffer.mkString).isEmpty
){
case TermState(rest, b, c, _) => BasicFilters.injectNewLine(b, c, rest)
}
Expand Down Expand Up @@ -134,15 +132,14 @@ case class AmmoniteFrontEnd(extraFilters: Filter = Filter.empty) extends FrontEn
displayTransform = { (buffer, cursor) =>


val indices = Parsers.defaultHighlightIndices(
buffer,
val highlighted = fansi.Str(parser.defaultHighlight(
buffer.toVector,
colors.comment(),
colors.`type`(),
colors.literal(),
colors.keyword(),
fansi.Attr.Reset
)
val highlighted = fansi.Str(Highlighter.flattenIndices(indices, buffer).mkString)
).mkString)
val (newBuffer, offset) = SelectionFilter.mangleBuffer(
selectionFilter, highlighted, cursor, colors.selected()
)
Expand Down
6 changes: 3 additions & 3 deletions amm/repl/src/main/scala/ammonite/repl/ApiImpls.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ object ClipboardImpl extends Clipboard {
trait FrontEndAPIImpl extends FrontEndAPI {
def apply(name: String): FrontEnd =
name.toLowerCase(Locale.ROOT) match {
case "ammonite" => AmmoniteFrontEnd()
case "windows" => FrontEnds.JLineWindows
case "unix" => FrontEnds.JLineUnix
case "ammonite" => AmmoniteFrontEnd(ammonite.compiler.Parsers)
case "windows" => new FrontEnds.JLineWindows(ammonite.compiler.Parsers)
case "unix" => new FrontEnds.JLineUnix(ammonite.compiler.Parsers)
case _ => throw new NoSuchElementException(s"Front-end $name")
}
}
6 changes: 3 additions & 3 deletions amm/repl/src/main/scala/ammonite/repl/FrontEndUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import ammonite.util.Util.newLine
* Created by haoyi on 8/29/15.
*/
object FrontEndUtils {
def width =
if (scala.util.Properties.isWin) ammonite.repl.FrontEnds.JLineWindows.width
def width =
if (scala.util.Properties.isWin) ammonite.repl.FrontEnds.width
else ammonite.terminal.ConsoleDim.width()
def height =
if (scala.util.Properties.isWin) ammonite.repl.FrontEnds.JLineWindows.width
if (scala.util.Properties.isWin) ammonite.repl.FrontEnds.height
else ammonite.terminal.ConsoleDim.height()
def tabulate(snippetsRaw: Seq[fansi.Str], width: Int): Iterator[String] = {
val gap = 2
Expand Down
25 changes: 14 additions & 11 deletions amm/repl/src/main/scala/ammonite/repl/FrontEnds.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import org.jline.reader.impl.history.DefaultHistory

import org.jline.terminal._
import org.jline.utils.AttributedString
import ammonite.compiler.iface.{Parser => IParser}
import ammonite.util.{Catching, Colors, Res}
import ammonite.repl.api.FrontEnd
import ammonite.compiler.Parsers
import org.jline.reader.impl.DefaultParser


object FrontEnds {
object JLineUnix extends JLineTerm
object JLineWindows extends JLineTerm
class JLineTerm() extends FrontEnd {
class JLineUnix(codeParser: IParser) extends JLineTerm(codeParser)
class JLineWindows(codeParser: IParser) extends JLineTerm(codeParser)
class JLineTerm(codeParser: IParser) extends FrontEnd {

private val term = TerminalBuilder.builder().build()

private val readerBuilder = LineReaderBuilder.builder().terminal(term)
private val ammHighlighter = new AmmHighlighter()
private val ammHighlighter = new AmmHighlighter(codeParser)
private val ammCompleter = new AmmCompleter(ammHighlighter)
private val ammParser = new AmmParser()
private val ammParser = new AmmParser(codeParser)
readerBuilder.highlighter(ammHighlighter)
readerBuilder.completer(ammCompleter)
readerBuilder.parser(ammParser)
Expand Down Expand Up @@ -79,6 +79,9 @@ object FrontEnds {
} yield res
}
}

def width = TerminalBuilder.builder().build().getWidth
def height = TerminalBuilder.builder().build().getHeight
}

class AmmCompleter(highlighter: Highlighter) extends Completer {
Expand Down Expand Up @@ -125,7 +128,7 @@ class AmmCompleter(highlighter: Highlighter) extends Completer {
}
}

class AmmParser extends Parser {
class AmmParser(codeParser: IParser) extends Parser {
class AmmoniteParsedLine(line: String, words: java.util.List[String],
wordIndex: Int, wordCursor: Int, cursor: Int,
val stmts: Seq[String] = Seq.empty // needed for interpreter
Expand All @@ -142,8 +145,8 @@ class AmmParser extends Parser {
val words = defParLine.words
val wordIndex = defParLine.wordIndex // index of the current word in the list of words
val wordCursor = defParLine.wordCursor // cursor position within the current word
Parsers.split(line) match {

codeParser.split(line) match {
case Some(Right(stmts)) =>
addHistory(line)
// if ENTER and not at the end of input -> newline
Expand Down Expand Up @@ -175,13 +178,13 @@ class AmmParser extends Parser {

class SyntaxError(val msg: String) extends RuntimeException

class AmmHighlighter extends Highlighter {
class AmmHighlighter(codeParser: IParser) extends Highlighter {

var colors: Colors = Colors.Default
def setErrorIndex(x$1: Int): Unit = ()
def setErrorPattern(x$1: java.util.regex.Pattern): Unit = ()
override def highlight(reader: LineReader, buffer: String): AttributedString = {
val hl = Parsers.defaultHighlight(
val hl = codeParser.defaultHighlight(
buffer.toVector,
colors.comment(),
colors.`type`(),
Expand Down
4 changes: 2 additions & 2 deletions amm/repl/src/main/scala/ammonite/repl/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class Repl(input: InputStream,

val frontEnd = Ref[FrontEnd](
if (scala.util.Properties.isWin)
ammonite.repl.FrontEnds.JLineWindows
new ammonite.repl.FrontEnds.JLineWindows(ammonite.compiler.Parsers)
else
AmmoniteFrontEnd(Filter.empty)
AmmoniteFrontEnd(ammonite.compiler.Parsers, Filter.empty)
)

var lastException: Throwable = null
Expand Down
3 changes: 2 additions & 1 deletion shell/src/main/scala/ammonite/shell/Configure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ object Configure {

def apply(interp: InterpAPI, repl: ReplAPI, wd: => ammonite.ops.Path) = {
if (scala.util.Properties.isWin) {
repl.frontEnd() = ammonite.repl.FrontEnds.JLineWindows
repl.frontEnd() = new ammonite.repl.FrontEnds.JLineWindows(ammonite.compiler.Parsers)
interp.colors() = ammonite.util.Colors.BlackWhite
} else {
repl.frontEnd() = ammonite.repl.AmmoniteFrontEnd(
ammonite.compiler.Parsers,
ammonite.shell.PathComplete.pathCompleteFilter(wd, interp.colors())
)
}
Expand Down

0 comments on commit 2f2b049

Please sign in to comment.