fix bugs related to parsing and loading file (did not implement new BNF grammar properly)
This commit is contained in:
@@ -2,7 +2,7 @@ structure Parser =
|
||||
struct
|
||||
structure T = Tokens
|
||||
|
||||
fun parseItems (tokens, grid) =
|
||||
fun parseItem (tokens, grid) =
|
||||
case tokens of
|
||||
T.L_BRACE ::
|
||||
T.INT x ::
|
||||
@@ -14,27 +14,48 @@ struct
|
||||
val colour = {r = r, g = g, b = b, a = a}
|
||||
val grid = ParseGrid.applyItem (grid, x, y, ex, ey, colour)
|
||||
in
|
||||
parseItems (tl, grid)
|
||||
SOME (tl, grid)
|
||||
end
|
||||
| _ => SOME (tokens, grid)
|
||||
| _ => NONE
|
||||
|
||||
(* note to be careful of:
|
||||
* - startParseItems returns NONE if there are no items found,
|
||||
* because we have not found a single item yet.
|
||||
*
|
||||
* - loopParseItems returns SOME if there are no items found,
|
||||
* because this function is called after we have parsed at least one item.
|
||||
* *)
|
||||
fun loopParseItems (tokens, grid) =
|
||||
case parseItem (tokens, grid) of
|
||||
SOME (tokens, grid) => loopParseItems (tokens, grid)
|
||||
| NONE => SOME (tokens, grid)
|
||||
|
||||
fun startParseItems (tokens, grid) =
|
||||
case parseItem (tokens, grid) of
|
||||
SOME (tokens, grid) => loopParseItems (tokens, grid)
|
||||
| NONE => NONE
|
||||
|
||||
fun parseLayers (tokens, canvasWidth, canvasHeight, tree, counter) =
|
||||
let
|
||||
val grid = ParseGrid.make (canvasWidth, canvasHeight)
|
||||
in
|
||||
case parseItems (tokens, grid) of
|
||||
SOME (tokens, grid) =>
|
||||
let val tree = LayerTree.insert (counter, grid, tree)
|
||||
in parseLayers (tokens, canvasWidth, canvasHeight, tree, counter + 1)
|
||||
end
|
||||
| NONE => (tokens, tree)
|
||||
end
|
||||
case tokens of
|
||||
T.L_BRACKET :: tl =>
|
||||
let
|
||||
val grid = ParseGrid.make (canvasWidth, canvasHeight)
|
||||
in
|
||||
case startParseItems (tl, grid) of
|
||||
SOME (T.R_BRACKET :: tl, grid) =>
|
||||
let val tree = LayerTree.insert (counter, grid, tree)
|
||||
in parseLayers (tl, canvasWidth, canvasHeight, tree, counter + 1)
|
||||
end
|
||||
| SOME _ => NONE
|
||||
| NONE => (tokens, tree)
|
||||
end
|
||||
| _ => (tokens, tree)
|
||||
|
||||
fun parse string =
|
||||
case Lexer.scan string of
|
||||
SOME tokens =>
|
||||
(case tokens of
|
||||
T.INT canvasWidth :: T.INT canvasHeight :: tl =>
|
||||
T.INT canvasWidth :: T.INT canvasHeight :: T.L_BRACE :: tl =>
|
||||
let
|
||||
val maxSide = Int.max (canvasWidth, canvasHeight)
|
||||
val tree = LayerTree.init maxSide
|
||||
|
||||
Reference in New Issue
Block a user