10000 Band-Aid the "Template Pattern Segment Unmatched" Error by iamrecursion · Pull Request #1509 · enso-org/enso · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Band-Aid the "Template Pattern Segment Unmatched" Error #1509

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

Merged
merged 3 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -996,4 +996,16 @@ class AstToIrTest extends CompilerTest with Inside {
}
}
}

"Bad macro invocations" should {
"result in syntax errors" in {
val ir =
"""import
|
|main = "foo"
|""".stripMargin.toIrModule

ir.bindings.head shouldBe an[IR.Error]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2319,14 +2319,20 @@ object AST {
val segsFail = !ctx.body.forall(_.isValid)
if (pfxFail || segsFail) unexpected(ctx, "invalid statement")
else {
val ctx2 = ctx.copy(
prefix = ctx.prefix.map(unapplyValidChecker),
body = unapplySegCheckers(ctx.body)
)
try resolver(ctx2)
catch {
try {
val ctx2 = ctx.copy(
prefix = ctx.prefix.map(unapplyValidChecker),
body = unapplySegCheckers(ctx.body)
)
resolver(ctx2)
} catch {
case _: Throwable =>
unexpected(ctx, "exception during macro resolution")
val tokens = ctx.body.flatMap(mat => {
val firstElem = Shifted(mat.head)
val rest = mat.body.toStream
firstElem :: rest
})
AST.Invalid.Unexpected("Unexpected tokens", tokens)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.enso.syntax.text.ast.meta

import org.enso.data
import org.enso.data.List1
import org.enso.data.Shifted
import org.enso.syntax.text.AST
import org.enso.syntax.text.AST.Ident
import org.enso.syntax.text.AST.Macro
import Pattern.streamShift
import cats.data.NonEmptyList
import org.enso.data
import org.enso.data.{List1, Shifted}
import org.enso.syntax.text.{AST, Shape}
import org.enso.syntax.text.AST.{Ident, Macro}
import org.enso.syntax.text.ast.meta.Pattern.streamShift

import scala.annotation.tailrec

Expand Down Expand Up @@ -194,8 +192,15 @@ object Builder {
val stream = revStream.reverse
pat.matchOpt(stream, lineBegin, reversed) match {
case None =>
throw new Error(
s"Internal error: template pattern segment was unmatched"
(
Shifted(
offset,
Shape.Match.Segment(
ast,
Pattern.Match.FailedMatch(Pattern.FailedMatch(None))
)
),
stream
)
case Some(rr) =>
(Shifted(offset, Match.Segment(ast, rr.elem)), rr.stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ object Pattern {
final case class Block (spaced : Spaced) extends P
final case class Macro (spaced : Spaced) extends P
final case class Invalid (spaced : Spaced) extends P

final case class FailedMatch(spaced: Spaced) extends P
// format: on

//// Smart Constructors ////
Expand Down Expand Up @@ -201,6 +203,8 @@ object Pattern {
final case class Block [T](pat:P.Block , elem:T) extends M[T]
final case class Macro [T](pat:P.Macro , elem:T) extends M[T]
final case class Invalid [T](pat:P.Invalid , elem:T) extends M[T]

final case class FailedMatch[T](pat:P.FailedMatch) extends M[T]
// format: on

//// Smart Constructors ////
Expand Down Expand Up @@ -235,29 +239,30 @@ object Pattern {
@nowarn("cat=unchecked")
def mapStructShallow(f: MatchOf[T] => MatchOf[T]): MatchOf[T] =
this match {
case m: M.Begin[T] => m
case m: M.End[T] => m
case m: M.Nothing[T] => m
case m: M.Seq[T] => m.copy(elem = m.elem.bimap(f, f))
case m: M.Or[T] => m.copy(elem = m.elem.bimap(f, f))
case m: M.Many[T] => m.copy(elem = m.elem.map(f))
case m: M.Except[T] => m.copy(elem = f(m.elem))
case m: M.Build[T] => m
case m: M.Err[T] => m
case m: M.Tag[T] => m.copy(elem = f(m.elem))
case m: M.Cls[T] => m.copy(elem = f(m.elem))
case m: M.Tok[T] => m
case m: M.Blank[T] => m
case m: M.Var[T] => m
case m: M.Cons[T] => m
case m: M.Opr[T] => m
case m: M.Annotation[T] => m
case m: M.Mod[T] => m
case m: M.Num[T] => m
case m: M.Text[T] => m
case m: M.Block[T] => m
case m: M.Macro[T] => m
case m: M.Invalid[T] => m
case m: M.Begin[T] => m
case m: M.End[T] => m
case m: M.Nothing[T] => m
case m: M.Seq[T] => m.copy(elem = m.elem.bimap(f, f))
case m: M.Or[T] => m.copy(elem = m.elem.bimap(f, f))
case m: M.Many[T] => m.copy(elem = m.elem.map(f))
case m: M.Except[T] => m.copy(elem = f(m.elem))
case m: M.Build[T] => m
case m: M.Err[T] => m
case m: M.Tag[T] => m.copy(elem = f(m.elem))
case m: M.Cls[T] => m.copy(elem = f(m.elem))
case m: M.Tok[T] => m
case m: M.Blank[T] => m
case m: M.Var[T] => m
case m: M.Cons[T] => m
case m: M.Opr[T] => m
case m: M.Annotation[T] => m
case m: M.Mod[T] => m
case m: M.Num[T] => m
case m: M.Text[T] => m
case m: M.Block[T] => m
case m: M.Macro[T] => m
case m: M.Invalid[T] => m
case m: M.FailedMatch[T] => m
}

@nowarn("cat=unchecked")
Expand Down Expand Up @@ -554,6 +559,10 @@ sealed trait Pattern {
case p @ P.Invalid(spaced) =>
matchByCls_[AST.Invalid](spaced, M.Invalid(p, _))

case _: P.FailedMatch =>
throw new RuntimeException(
"Should not occur during pattern matching."
)
}
}
step(this, stream0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,3 @@ class ParserTest extends AnyFlatSpec with Matchers {
// [ ] warnings in scala code
// [ ] Undefined parsing
// [ ] All block types
// [ ] Unary minus
0