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

Comment rules #2

Merged
merged 2 commits into from
Feb 14, 2025
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
13 changes: 9 additions & 4 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import Catlab.Parsers.ParserCore
export @decapode_str

# Bodies are made up of lines where each line holds a statement
@rule DecapodeExpr = Line[*] & ws |> v -> BuildExpr(v[1])
@rule DecapodeExpr = (MultiLineComment, Line)[*] & ws |> v -> BuildExpr(v[1])

# Lines are made up of a statement followed by an end of line character.
@rule Line = ws & Statement & r"[^\S\r\n]*" & EOL |> v->v[2]
#Comments are ignored by the parser.
#==#@rule SingleLineComment = "#" & r"[^\r\n]*" |> v -> nothing
@rule MultiLineComment = "#=" & r"(?:[^=]|=(?!#)|\s)*" & "=#" |> v -> nothing

# Lines are made up of a statement or comment followed by an end of line character.
@rule Line = ws & (SingleLineComment , Statement) & r"[^\S\r\n]*" & EOL |> v -> v[2]

# Statements can include either type judgements or equations.
@rule Statement = Judgement , Equation
Expand Down Expand Up @@ -123,9 +127,10 @@ BuildExpr
Takes in an input array (AST) for a body and returns a corresponding DecaExpr object.
"""
function BuildExpr(v)
filtered_lines = filter(x -> x !== nothing, v)
judges = []
eqns = []
foreach(v) do s
foreach(filtered_lines) do s
@match s begin
::Judgement => push!(judges, s)
::Vector{Judgement} => append!(judges, s)
Expand Down
19 changes: 18 additions & 1 deletion test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Test
using Catlab
using DiagrammaticEquations
using DiagrammaticEquations: Term, Derivative, PlusOperation, MultOperation, Call, Args,
Judgement, Statement, Line, Equation, List, Compose, TypeName, Grouping, DecapodeExpr
Judgement, Statement, Line, Equation, List, Compose, TypeName, Grouping, DecapodeExpr, SingleLineComment, MultiLineComment

PEG.setdebug!(false) # To disable: PEG.setdebug!(false)

Expand All @@ -21,6 +21,23 @@ PEG.setdebug!(false) # To disable: PEG.setdebug!(false)
],[])
end

@testset "Comments" begin
@test DecapodeExpr("a::b\n# This is a comment\n")[1] == DecaExpr([DiagrammaticEquations.decapodes.Judgement(:a, :b, :I)], [])
@test DecapodeExpr("a::b\n#a::b\n")[1] == DecaExpr([DiagrammaticEquations.decapodes.Judgement(:a, :b, :I)], [])
@test DecapodeExpr("# This is a comment\na::b\n")[1] == DecaExpr([DiagrammaticEquations.decapodes.Judgement(:a, :b, :I)], [])
@test DecapodeExpr("a::b\n# This is comment\nc == d\n")[1] == DecaExpr([
DiagrammaticEquations.decapodes.Judgement(:a, :b, :I)],
[DiagrammaticEquations.decapodes.Eq(DiagrammaticEquations.decapodes.Var(:c), DiagrammaticEquations.decapodes.Var(:d))])
@test DecapodeExpr("#= This is a multi-line comment\nspanning multiple lines\n=# a::b\n")[1] == DecaExpr([
DiagrammaticEquations.decapodes.Judgement(:a, :b, :I)
], [])
@test DecapodeExpr("a::b\n#= Multi-line comment\nspanning lines\n=#\nc == d\n")[1] == DecaExpr([
DiagrammaticEquations.decapodes.Judgement(:a, :b, :I)
], [
DiagrammaticEquations.decapodes.Eq(DiagrammaticEquations.decapodes.Var(:c), DiagrammaticEquations.decapodes.Var(:d))
])
end

@testset "Line" begin
@test Line("a::b\n")[1] == DiagrammaticEquations.decapodes.Judgement(:a, :b, :I)
@test Line("alpha::beta\n")[1] == DiagrammaticEquations.decapodes.Judgement(:alpha, :beta, :I)
Expand Down