Skip to content
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
12 changes: 8 additions & 4 deletions src/read_MTG/read_MTG.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ end
function parse_mtg_file(f, mtg_type)
line = [0]
l = [""]
classes = nothing
description = nothing
features = nothing
mtg = nothing
l[1] = next_line!(f, line)

while !eof(f)
Expand All @@ -95,15 +99,15 @@ function parse_mtg_file(f, mtg_type)

# Parse the mtg CLASSES section, and then continue to next while loop iteration:
if issection(l[1], "CLASSES")
global classes = parse_section!(f, ["SYMBOL", "SCALE", "DECOMPOSITION", "INDEXATION", "DEFINITION"], "CLASSES", line, l)
classes = parse_section!(f, ["SYMBOL", "SCALE", "DECOMPOSITION", "INDEXATION", "DEFINITION"], "CLASSES", line, l)
classes.SCALE = parse.(Int, classes.SCALE)
replace!(classes.SYMBOL, "\$" => "Scene")
continue
end

# Parse the mtg DESCRIPTION section:
if issection(l[1], "DESCRIPTION")
global description = parse_section!(f, ["LEFT", "RIGHT", "RELTYPE", "MAX"], "DESCRIPTION", line, l, allow_empty=true)
description = parse_section!(f, ["LEFT", "RIGHT", "RELTYPE", "MAX"], "DESCRIPTION", line, l, allow_empty=true)
if description !== nothing
description.RIGHT = split.(description.RIGHT, ",")
if !all([i in description.RELTYPE for i in ("+", "<")])
Expand All @@ -116,13 +120,13 @@ function parse_mtg_file(f, mtg_type)

# Parse the mtg FEATURES section:
if issection(l[1], "FEATURES")
global features = parse_section!(f, ["NAME", "TYPE"], "FEATURES", line, l, allow_empty=true)
features = parse_section!(f, ["NAME", "TYPE"], "FEATURES", line, l, allow_empty=true)
continue
end

# Parse the mtg FEATURES section:
if issection(l[1], "MTG")
global mtg = parse_mtg!(f, classes, features, line, l, mtg_type)
mtg = parse_mtg!(f, classes, features, line, l, mtg_type)
continue
end

Expand Down
21 changes: 21 additions & 0 deletions test/test-read_mtg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,24 @@ end
!MultiScaleTreeGraph.isroot(x) && @test isempty(node_attributes(x))
end
end

@testset "read_mtg parser state is local" begin
parser_state_names = (:classes, :description, :features, :mtg)
@test all(name -> !isdefined(MultiScaleTreeGraph, name), parser_state_names)

files = [
"files/simple_plant.mtg",
"files/simple_plant-blanks.mtg",
"files/simple_plant-P1U1.mtg",
"files/palm.mtg",
]

for file in files
mtg = read_mtg(file, NodeMTG)
@test node_id(mtg) == 1
@test mtg[:symbols] isa Vector{String}
@test mtg[:scales] isa Vector{Int}
end

@test all(name -> !isdefined(MultiScaleTreeGraph, name), parser_state_names)
end
Loading