|
| 1 | +moduleInternal= { |
| 2 | +moduleType= { |
| 3 | +letanPlusB="AnPlusB"; |
| 4 | +letatrule="Atrule"; |
| 5 | +letatrulePrelude="AtrulePrelude"; |
| 6 | +letattributeSelector="AttributeSelector"; |
| 7 | +letblock="Block"; |
| 8 | +letbrackets="Brackets"; |
| 9 | +letcdc="CDC"; |
| 10 | +letcdo="CDO"; |
| 11 | +letclassSelector="ClassSelector"; |
| 12 | +letcombinator="Combinator"; |
| 13 | +letcomment="Comment"; |
| 14 | +letdeclaration="Declaration"; |
| 15 | +letdeclarationList="DeclarationList"; |
| 16 | +letdimension="Dimension"; |
| 17 | +letfunction_="Function"; |
| 18 | +lethexColor="HexColor"; |
| 19 | +letidentifier="Identifier"; |
| 20 | +letidSelector="IdSelector"; |
| 21 | +letmediaFeature="MediaFeature"; |
| 22 | +letmediaQuery="MediaQuery"; |
| 23 | +letmediaQueryList="MediaQueryList"; |
| 24 | +letnth="Nth"; |
| 25 | +letnumber="Number"; |
| 26 | +letoperator="Operator"; |
| 27 | +letparentheses="Parentheses"; |
| 28 | +letpercentage="Percentage"; |
| 29 | +letpseudoClassSelector="PseudoClassSelector"; |
| 30 | +letpseudoElementSelector="PseudoElementSelector"; |
| 31 | +letratio="Ratio"; |
| 32 | +letraw="Raw"; |
| 33 | +letrule="Rule"; |
| 34 | +letselector="Selector"; |
| 35 | +letselectorList="SelectorList"; |
| 36 | +letstring="String"; |
| 37 | +letstyleSheet="StyleSheet"; |
| 38 | +lettypeSelector="TypeSelector"; |
| 39 | +letunicodeRange="UnicodeRange"; |
| 40 | +leturl="Url"; |
| 41 | +letvalue="Value"; |
| 42 | +letwhiteSpace="WhiteSpace"; |
| 43 | + }; |
| 44 | +typenode= {. "type":string}; |
| 45 | +typeast; |
| 46 | + [@bs.module"css-tree"]externalparse:string =>ast=""; |
| 47 | + [@bs.module"css-tree"]externaltoPlainObject:ast =>node=""; |
| 48 | +}; |
| 49 | + |
| 50 | +typepoint= { |
| 51 | + offset:int, |
| 52 | + line:int, |
| 53 | + column:int |
| 54 | +}; |
| 55 | + |
| 56 | +typeloc= { |
| 57 | + source:string, |
| 58 | + start:point, |
| 59 | + end_:point |
| 60 | +}; |
| 61 | + |
| 62 | +typestyleSheet= { |
| 63 | + loc:option(loc), |
| 64 | + children:array(node) |
| 65 | +} |
| 66 | +andrule= { |
| 67 | + loc:option(loc), |
| 68 | + prelude:node, |
| 69 | + block:node |
| 70 | +} |
| 71 | +and selectorList = { |
| 72 | +loc: option(loc), |
| 73 | + children: array(node) |
| 74 | +} |
| 75 | +and selector= { |
| 76 | + loc: option(loc), |
| 77 | + children: array(node) |
| 78 | +} |
| 79 | +and classSelector= { |
| 80 | + loc: option(loc), |
| 81 | + name: string |
| 82 | +} |
| 83 | +and node= |
| 84 | + |StyleSheet(styleSheet) |
| 85 | + |Rule(rule) |
| 86 | + |SelectorList(selectorList) |
| 87 | + |Selector(selector) |
| 88 | + |ClassSelector(classSelector) |
| 89 | + |Unknown(string); |
| 90 | + |
| 91 | +moduleDecoder= { |
| 92 | +letpoint= json=> |
| 93 | +Json.Decode.{ |
| 94 | + offset: json|> field("offset", int), |
| 95 | + line: json|> field("line", int), |
| 96 | + column: json|> field("column", int) |
| 97 | + }; |
| 98 | +letloc= json=> |
| 99 | +Json.Decode.{ |
| 100 | + source: json|> field("source", string), |
| 101 | + start: json|> field("start", point), |
| 102 | + end_: json|> field("end", point) |
| 103 | + }; |
| 104 | +externalnodeToJson:Internal.node =>Js.Json.t="%identity"; |
| 105 | +letrecdecoders=[ |
| 106 | + (Internal.Type.styleSheet, styleSheet), |
| 107 | + (Internal.Type.rule, rule), |
| 108 | + (Internal.Type.selectorList, selectorList), |
| 109 | + (Internal.Type.selector, selector), |
| 110 | + (Internal.Type.classSelector, classSelector) |
| 111 | +] |
| 112 | +andnode= json=> { |
| 113 | +letkind= json|>Json.Decode.field("type",Json.Decode.string); |
| 114 | +letdecoder= |
| 115 | +try (List.assoc(kind, decoders)) { |
| 116 | +|_=> unknown |
| 117 | + }; |
| 118 | + decoder(json); |
| 119 | + } |
| 120 | +andstyleSheet= json=> |
| 121 | +StyleSheet( |
| 122 | +Json.Decode.{ |
| 123 | + children: json|> field("children", array(node)), |
| 124 | + loc: json|> optional(field("loc", loc)) |
| 125 | + } |
| 126 | + ) |
| 127 | +andrule= json=> |
| 128 | +Rule( |
| 129 | +Json.Decode.{ |
| 130 | + loc: json|> optional(field("loc", loc)), |
| 131 | + prelude: json|> field("prelude", node), |
| 132 | + block: json|> field("block", node) |
| 133 | + } |
| 134 | + ) |
| 135 | +andselectorList= json=> |
| 136 | +SelectorList( |
| 137 | +Json.Decode.{ |
| 138 | + loc: json|> optional(field("loc", loc)), |
| 139 | + children: json|> field("children", array(node)) |
| 140 | + } |
| 141 | + ) |
| 142 | +andselector= json=> |
| 143 | +Selector( |
| 144 | +Json.Decode.{ |
| 145 | + loc: json|> optional(field("loc", loc)), |
| 146 | + children: json|> field("children", array(node)) |
| 147 | + } |
| 148 | + ) |
| 149 | +andclassSelector= json=> |
| 150 | +ClassSelector( |
| 151 | +Json.Decode.{ |
| 152 | + loc: json|> optional(field("loc", loc)), |
| 153 | + name: json|> field("name", string) |
| 154 | + } |
| 155 | + ) |
| 156 | +andunknown= json=> { |
| 157 | +letkind=Json.Decode.field("type",Json.Decode.string, json); |
| 158 | +Unknown(kind); |
| 159 | + }; |
| 160 | +letdecode= cssNode=> { |
| 161 | +letjson= nodeToJson(cssNode); |
| 162 | + node(json); |
| 163 | + }; |
| 164 | +}; |
| 165 | + |
| 166 | +letparse= (_fileName:string, source:string)=> { |
| 167 | +letast=Internal.parse(source); |
| 168 | +letastObj=Internal.toPlainObject(ast); |
| 169 | +Decoder.decode(astObj); |
| 170 | +}; |