Skip to content

Commit

Permalink
Merge pull request #358 from alephium/parser_updates
Browse files Browse the repository at this point in the history
Parser updates
  • Loading branch information
simerplaha authored Jan 21, 2025
2 parents 37d411a + d6ac479 commit 7fc5e41
Show file tree
Hide file tree
Showing 41 changed files with 510 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private object AnnotationParser {
SpaceParser.parseOrFail.? ~
IdentifierParser.parse ~
SpaceParser.parseOrFail.? ~
TupleParser.parseOrFail.? ~
ParameterParser.parseOrFail.? ~
SpaceParser.parseOrFail.? ~
Index
} map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private case object AssignmentParser {
def parseOrFail[Unknown: P]: P[SoftAST.Assignment] =
P {
Index ~
ExpressionParser.parseOrFailSelective(parseInfix = true, parseMethodCall = true, parseAssignment = false) ~
leftExpression ~
SpaceParser.parseOrFail.? ~
TokenParser.parseOrFail(Token.Equal) ~
SpaceParser.parseOrFail.? ~
Expand All @@ -28,4 +28,20 @@ private case object AssignmentParser {
)
}

private def leftExpression[Unknown: P]: P[SoftAST.ExpressionAST] =
P {
InfixCallParser.parseOrFail |
MethodCallParser.parseOrFail |
BlockParser.parseOrFail |
VariableDeclarationParser.parseOrFail |
MutableBindingParser.parseOrFail |
ReferenceCallParser.parseOrFail |
AnnotationParser.parseOrFail |
ParameterParser.parseOrFail |
NumberParser.parseOrFail |
BooleanParser.parseOrFail |
BStringParser.parseOrFail |
IdentifierParser.parseOrFail
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.{SoftAST, Token}

private object BlockParser {

def clause[Unknown: P](required: Boolean): P[SoftAST.BlockClause] =
def parse[Unknown: P]: P[SoftAST.BlockClause] =
parse(required = true)

def parseOrFail[Unknown: P]: P[SoftAST.BlockClause] =
parse(required = false)

def body[Unknown: P]: P[SoftAST.BlockBody] =
body()

private def parse[Unknown: P](required: Boolean): P[SoftAST.BlockClause] =
P {
Index ~
TokenParser.parse(required, Token.OpenCurly) ~
Expand All @@ -44,9 +53,6 @@ private object BlockParser {
)
}

def body[Unknown: P]: P[SoftAST.BlockBody] =
body()

private def body[Unknown: P](stop: Token*): P[SoftAST.BlockBody] =
P {
Index ~
Expand Down Expand Up @@ -80,7 +86,8 @@ private object BlockParser {
private def part[Unknown: P](stop: Seq[Token]): P[SoftAST.BodyPartAST] =
P {
TemplateParser.parseOrFail |
DataTemplateParser.parseOrFail |
EventTemplateParser.parseOrFail |
StructTemplateParser.parseOrFail |
FunctionParser.parseOrFail |
ExpressionParser.parseOrFail |
CommentParser.parseOrFail |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.alephium.ralph.lsp.access.compiler.parser.soft
import fastparse._
import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.{SoftAST, Token}

object BooleanParser {
private object BooleanParser {

def parseOrFail[Unknown: P]: P[SoftAST.TokenExpression[Token.PrimitiveBoolean]] =
P(boolean) map (SoftAST.TokenExpression(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fastparse.NoWhitespace.noWhitespaceImplicit
import org.alephium.ralph.lsp.access.compiler.message.SourceIndexExtra.range
import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.{SoftAST, Token}

object CodeParser {
private object CodeParser {

def parseOrFail[Unknown: P, T <: Token](token: T): P[SoftAST.CodeToken[T]] =
P(Index ~ token.lexeme ~ Index) map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ object Demo extends App {
val ast =
compiler.parseSoft {
"""
|event Event(a: Type)
|struct Struct { a: Type }
|
|Contract HelloWorld(type: SomeType, tuple: (A, B)) extends Class implements Trait {
|
| // This multiline comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import fastparse.NoWhitespace.noWhitespaceImplicit
import org.alephium.ralph.lsp.access.compiler.message.SourceIndexExtra.range
import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.{SoftAST, Token}

private object DataTemplateParser {
private object EventTemplateParser {

def parseOrFail[Unknown: P]: P[SoftAST.DataTemplate] =
def parseOrFail[Unknown: P]: P[SoftAST.EventTemplate] =
P {
Index ~
(TokenParser.parseOrFail(Token.Struct) | TokenParser.parseOrFail(Token.Enum) | TokenParser.parseOrFail(Token.Event)) ~
TokenParser.parseOrFail(Token.Event) ~
SpaceParser.parse ~
IdentifierParser.parse ~
SpaceParser.parseOrFail.? ~
ParameterParser.parse ~
Index
} map {
case (from, templateType, preIdentifierSpace, identifier, preParamSpace, params, to) =>
SoftAST.DataTemplate(
case (from, eventToken, preIdentifierSpace, identifier, preParamSpace, params, to) =>
SoftAST.EventTemplate(
index = range(from, to),
dataType = templateType,
eventToken = eventToken,
preIdentifierSpace = preIdentifierSpace,
identifier = identifier,
preParamSpace = preParamSpace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,9 @@ import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.SoftAST
private object ExpressionParser {

def parse[Unknown: P]: P[SoftAST.ExpressionAST] =
parseSelective(
parseInfix = true,
parseMethodCall = true,
parseAssignment = true
)

def parseOrFail[Unknown: P]: P[SoftAST.ExpressionAST] =
parseOrFailSelective(
parseInfix = true,
parseMethodCall = true,
parseAssignment = true
)

def parseSelective[Unknown: P](
parseInfix: Boolean,
parseMethodCall: Boolean,
parseAssignment: Boolean): P[SoftAST.ExpressionAST] =
P {
Index ~
parseOrFailSelective(
parseInfix = parseInfix,
parseMethodCall = parseMethodCall,
parseAssignment = parseAssignment
).? ~
parseOrFail.? ~
Index
} map {
case (_, Some(expression), _) =>
Expand All @@ -57,48 +36,21 @@ private object ExpressionParser {
SoftAST.ExpressionExpected(range(from, to))
}

def parseOrFailSelective[Unknown: P](
parseInfix: Boolean,
parseMethodCall: Boolean,
parseAssignment: Boolean): P[SoftAST.ExpressionAST] = {
def infixOrFail() =
if (parseInfix)
InfixCallParser.parseOrFail
else
Fail(s"${InfixCallParser.productPrefix} ignored")

def methodCallOrFail() =
if (parseMethodCall)
MethodCallParser.parseOrFail
else
Fail(s"${MethodCallParser.productPrefix} ignored")

def assignmentOrFail() =
if (parseAssignment)
AssignmentParser.parseOrFail
else
Fail(s"${AssignmentParser.productPrefix} ignored")

P {
assignmentOrFail() |
infixOrFail() |
methodCallOrFail() |
common
}
}

private def common[Unknown: P]: P[SoftAST.ExpressionAST] =
def parseOrFail[Unknown: P]: P[SoftAST.ExpressionAST] =
P {
ReturnStatementParser.parseOrFail |
TypeAssignmentParser.parseOrFail |
AssignmentParser.parseOrFail |
InfixCallParser.parseOrFail |
MethodCallParser.parseOrFail |
BlockParser.parseOrFail |
ReturnStatementParser.parseOrFail |
ForLoopParser.parseOrFail |
WhileLoopParser.parseOrFail |
VariableDeclarationParser.parseOrFail |
MutableBindingParser.parseOrFail |
TypeAssignmentParser.parseOrFail |
BlockParser.clause(required = false) |
ReferenceCallParser.parseOrFail |
AnnotationParser.parseOrFail |
TupleParser.parseOrFail |
ParameterParser.parseOrFail |
NumberParser.parseOrFail |
BooleanParser.parseOrFail |
BStringParser.parseOrFail |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private object ForLoopParser {
SpaceParser.parseOrFail.? ~
TokenParser.parse(Token.CloseParen) ~
SpaceParser.parseOrFail.? ~
BlockParser.clause(required = true) ~
BlockParser.parse ~
Index
} map {
case (from,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package org.alephium.ralph.lsp.access.compiler.parser.soft

import fastparse._
import fastparse.NoWhitespace.noWhitespaceImplicit
import org.alephium.ralph.lsp.access.compiler.message.SourceIndexExtra.range
import org.alephium.ralph.lsp.access.compiler.message.SourceIndexExtra.{point, range}
import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.{SoftAST, Token}

private object FunctionParser {
Expand All @@ -39,7 +39,7 @@ private object FunctionParser {
SpaceParser.parse ~
signature ~
SpaceParser.parseOrFail.? ~
BlockParser.clause(required = false).? ~
BlockParser.parseOrFail.? ~
Index
} map {
case (from, annotation, postAnnotationSpace, pub, fnDeceleration, headSpace, signature, tailSpace, block, to) =>
Expand Down Expand Up @@ -92,7 +92,9 @@ private object FunctionParser {
private def returnSignature[Unknown: P]: P[SoftAST.FunctionReturnAST] =
P {
Index ~
(TokenParser.parse(Token.ForwardArrow) ~ SpaceParser.parseOrFail.? ~ TypeParser.parse).? ~
(TokenParser.parse(Token.ForwardArrow) ~
SpaceParser.parseOrFail.? ~
returnExpression).? ~
Index
} map {
case (from, Some((forwardArrow, space, tpe)), to) =>
Expand All @@ -107,4 +109,20 @@ private object FunctionParser {
SoftAST.FunctionReturnExpected(range(from, to))
}

private def returnExpression[Unknown: P]: P[SoftAST.ExpressionAST] =
P(Index ~ returnExpressionParserOrFail.?) map {
case (_, Some(expression)) =>
expression

case (from, None) =>
SoftAST.ExpressionExpected(point(from))
}

private def returnExpressionParserOrFail[Unknown: P]: P[SoftAST.ExpressionAST] =
P {
ParameterParser.parseOrFail |
NumberParser.parseOrFail |
IdentifierParser.parseOrFail
}

}
Loading

0 comments on commit 7fc5e41

Please sign in to comment.