Skip to content

Commit

Permalink
Additional fixes for parser crashes (#1523)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamrecursion committed Mar 2, 2021
1 parent 77db384 commit 1eb62d4
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ trait CompilerRunner {

parser.dropMacroMeta(unresolvedAST)
}

/** Produces the [[AST]] representation of [[source]] without dropping the
* macro metadata.
*
* @return [[source]] as an AST
*/
def toAstWithMeta: AST = {
val parser: Parser = Parser()
parser.runWithIds(source)
}
}

/** An extension method to allow converting string source code to IR as a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ class AstToIrTest extends CompilerTest with Inside {
}

"Bad macro invocations" should {
"result in syntax errors" in {
"result in syntax errors for imports" in {
val ir =
"""import
|
Expand All @@ -1011,5 +1011,21 @@ class AstToIrTest extends CompilerTest with Inside {

ir.bindings.head shouldBe an[IR.Error]
}

"result in syntax errors for lambdas with no body" in {
val ir =
"""a ->
|""".stripMargin.toIrExpression.get

ir shouldBe an[IR.Error]
}

"result in syntax errors for lambdas with no args" in {
val ir =
"""-> a
|""".stripMargin.toIrExpression.get

ir shouldBe an[IR.Error]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ object Shape extends ShapeImplicit {
final case class Match[T](
pfx: Option[Pattern.Match],
segs: Shifted.List1[Match.Segment[T]],
resolved: AST
resolved: Option[AST]
) extends Macro[T] {
def path: List1[AST] = segs.toList1().map(_.wrapped.head)
}
Expand Down Expand Up @@ -2153,7 +2153,7 @@ object AST {
def apply(
pfx: Option[Pattern.Match],
segs: Shifted.List1[Match.Segment],
resolved: AST
resolved: Option[AST]
): Match = Shape.Match[AST](pfx, segs, resolved)

type Segment = Shape.Match.Segment[AST]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ final class Builder(
}

// val resolved = mdef.fin(pfxMatch, shiftSegs.toList().map(_.el))
val template = Macro.Match(pfxMatch, shiftSegs, null)
val template = Macro.Match(pfxMatch, shiftSegs, None)
val newTok = Shifted(segs2.head.off, template)

(newLeftStream, newTok, tailStream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ object Builtin {
t match {
case AST.App.Prefix(_, arg) => arg
case AST.App.Infix(self, _, _) => go(self)
case AST.Macro.Match.any(m) => go(m.resolved)
case AST.Macro.Match.any(m) => go(m.resolved.orNull)
case AST.Group(None) => t
case AST.Group(Some(s)) => go(s)
case _ => t
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package org.enso.syntax.text.ast.meta

import org.enso.syntax.text.{AST, HasSpan, OffsetZip}
import org.enso.data.{Index, Shifted}
import org.enso.syntax.text.AST.SAST
import org.enso.syntax.text.prec.Operator
import org.enso.syntax.text.ast.Repr
import org.enso.syntax.text.prec.Operator
import org.enso.syntax.text.{AST, HasSpan, OffsetZip}

import scala.annotation.{nowarn, tailrec}
import org.enso.data.Index
import org.enso.data.Shifted

////////////////////////////////////////////////////////////////////////////////
//// Pattern ///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

object Pattern {
import cats.Foldable
import cats.Functor
import cats.Traverse
import cats.{Foldable, Functor, Traverse}
import cats.derived._

type P = Pattern
Expand Down Expand Up @@ -401,8 +398,8 @@ sealed trait Pattern {
reversed: Boolean = false
): Match.Result = {
matchOpt(stream, lineBegin, reversed).getOrElse {
val msg = "Internal error: template pattern segment was unmatched"
throw new Error(msg)
val badMatch: Match = Match.FailedMatch(FailedMatch(None))
Match.Result(badMatch, stream)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class Parser {
val segments = resolvedAST.segs.toList().map(_.wrapped)
val ctx = AST.Macro.Resolver.Context(resolvedAST.pfx, segments, id)
resolvedAST.copy(shape = resolvedAST.shape.copy[AST](resolved = {
resolveMacros(spec.resolver(ctx))
Option(resolveMacros(spec.resolver(ctx)))
}))
}
case _ => ast.map(resolveMacros)
Expand Down Expand Up @@ -404,8 +404,8 @@ class Parser {
val originalSegments = (prefix ++ segments).map(deriveLocations)
val originalSpan =
Foldable[List].foldMap(originalSegments)(_.location)
val resolved = t.resolved.setLocation(originalSpan)
go(resolved)
val resolved = t.resolved.map(_.setLocation(originalSpan))
go(resolved.orNull)
}
case t => deriveLocation(t.map(go))
}
Expand Down

0 comments on commit 1eb62d4

Please sign in to comment.