Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional Parser Bugfixes #1523

Merged
merged 4 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -998,7 +998,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 @@ -1007,5 +1007,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 @@ -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