|
| 1 | +///| |
| 2 | +pub(all) enum Ast { |
| 3 | + Null(Location) |
| 4 | + False(Location) |
| 5 | + True(Location) |
| 6 | + Str(str~ : String, loc~ : Location) |
| 7 | + Float(float~ : String, loc~ : Location) |
| 8 | + Arr(content~ : Array[Ast], loc~ : Location) |
| 9 | + Obj(map~ : Map[String, Ast], loc~ : Location) |
| 10 | +} derive(Debug, Eq) |
| 11 | + |
| 12 | +///| |
| 13 | +pub fn Ast::loc(self : Self) -> Location { |
| 14 | + match self { |
| 15 | + Null(loc) => loc |
| 16 | + False(loc) => loc |
| 17 | + True(loc) => loc |
| 18 | + Str(loc~, ..) => loc |
| 19 | + Float(loc~, ..) => loc |
| 20 | + Arr(loc~, ..) => loc |
| 21 | + Obj(loc~, ..) => loc |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +///| |
| 26 | +pub fn Ast::as_bool(self : Self) -> Bool? { |
| 27 | + match self { |
| 28 | + True(_) => Some(true) |
| 29 | + False(_) => Some(false) |
| 30 | + _ => None |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +///| |
| 35 | +pub fn Ast::as_string(self : Self) -> String? { |
| 36 | + match self { |
| 37 | + Str(str~, ..) => Some(str) |
| 38 | + _ => None |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +///| |
| 43 | +pub fn Ast::as_number(self : Self) -> String? { |
| 44 | + match self { |
| 45 | + Float(float~, ..) => Some(float) |
| 46 | + _ => None |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +///| |
| 51 | +pub fn Ast::as_array(self : Self) -> Array[Ast]? { |
| 52 | + match self { |
| 53 | + Arr(content~, ..) => Some(content) |
| 54 | + _ => None |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +///| |
| 59 | +pub fn Ast::as_object(self : Self) -> Map[String, Ast]? { |
| 60 | + match self { |
| 61 | + Obj(map~, ..) => Some(map) |
| 62 | + _ => None |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +///| |
| 67 | +pub impl ToJson for Ast with to_json(self) { |
| 68 | + match self { |
| 69 | + Null(loc) => { "type": "Null", "loc": loc } |
| 70 | + False(loc) => { "type": "False", "loc": loc } |
| 71 | + True(loc) => { "type": "True", "loc": loc } |
| 72 | + Str(str~, loc~) => { "type": "Str", "str": str, "loc": loc } |
| 73 | + Float(float~, loc~) => { "type": "Float", "float": float, "loc": loc } |
| 74 | + Arr(content~, loc~) => { "type": "Arr", "content": content, "loc": loc } |
| 75 | + Obj(map~, loc~) => { "type": "Obj", "map": map, "loc": loc } |
| 76 | + } |
| 77 | +} |
0 commit comments