SassyJSON 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +18 -18
  3. data/README.md +112 -112
  4. data/lib/JsonImporter.rb +228 -228
  5. data/lib/SassyJSON.rb +12 -14
  6. data/stylesheets/SassyJSON.scss +5 -5
  7. data/stylesheets/decode/api/_json.scss +26 -26
  8. data/stylesheets/decode/decode.scss +28 -28
  9. data/stylesheets/decode/helpers/all/_throw.scss +11 -11
  10. data/stylesheets/decode/helpers/all/_value.scss +49 -49
  11. data/stylesheets/decode/helpers/color/_color.scss +50 -50
  12. data/stylesheets/decode/helpers/color/_get-color-value.scss +22 -22
  13. data/stylesheets/decode/helpers/color/_hex-to-dec.scss +19 -19
  14. data/stylesheets/decode/helpers/color/_hex.scss +39 -39
  15. data/stylesheets/decode/helpers/color/_hsl.scss +46 -46
  16. data/stylesheets/decode/helpers/color/_rgb.scss +46 -46
  17. data/stylesheets/decode/helpers/map/_consume.scss +33 -33
  18. data/stylesheets/decode/helpers/number/_find-digits.scss +39 -39
  19. data/stylesheets/decode/helpers/number/_find-exponent.scss +51 -51
  20. data/stylesheets/decode/helpers/number/_find-integer.scss +37 -37
  21. data/stylesheets/decode/helpers/number/_pow.scss +22 -22
  22. data/stylesheets/decode/helpers/string/_find-ending-quote.scss +57 -57
  23. data/stylesheets/decode/helpers/string/_length.scss +42 -42
  24. data/stylesheets/decode/helpers/string/_strip-token.scss +16 -16
  25. data/stylesheets/decode/types/_bool.scss +40 -40
  26. data/stylesheets/decode/types/_list.scss +54 -54
  27. data/stylesheets/decode/types/_map.scss +78 -78
  28. data/stylesheets/decode/types/_null.scss +19 -19
  29. data/stylesheets/decode/types/_number.scss +63 -63
  30. data/stylesheets/decode/types/_string.scss +41 -41
  31. data/stylesheets/encode/api/_json.scss +17 -17
  32. data/stylesheets/encode/encode.scss +17 -17
  33. data/stylesheets/encode/helpers/_quote.scss +9 -9
  34. data/stylesheets/encode/mixins/_json.scss +36 -36
  35. data/stylesheets/encode/types/_bool.scss +9 -9
  36. data/stylesheets/encode/types/_color.scss +9 -9
  37. data/stylesheets/encode/types/_list.scss +13 -13
  38. data/stylesheets/encode/types/_map.scss +13 -13
  39. data/stylesheets/encode/types/_null.scss +9 -9
  40. data/stylesheets/encode/types/_number.scss +9 -9
  41. data/stylesheets/encode/types/_string.scss +9 -9
  42. metadata +8 -10
@@ -1,78 +1,78 @@
1
- // Parses a JSON encoded object
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $source: JSON complete source
4
- // @param [number] $pointer: current pointer
5
- // --------------------------------------------------------------------------------
6
- // @throw "Unexpected comma in object literal."
7
- // @throw "Unexpected token $token in object literal."
8
- // @throw "Missing comma in object literal."
9
- // @throw "Unterminated object literal."
10
- // @throw "Consuming token `:` failed."
11
- // --------------------------------------------------------------------------------
12
- // @return [list|false] (new pointer, map)
13
-
14
- @function _json-decode--map($source, $pointer) {
15
- $length: str-length($source);
16
- $map: ();
17
- $needs-comma: false;
18
-
19
- // Deal with empty map
20
- @if $pointer <= $length and str-slice($source, $pointer, $pointer) == "}" {
21
- @return ($pointer + 1, $map);
22
- }
23
-
24
- @while $pointer <= $length {
25
- $token: str-slice($source, $pointer, $pointer);
26
- $pointer: $pointer + 1;
27
-
28
- @if $token == "}" {
29
- @if not $needs-comma and length($map) != 0 {
30
- @return _throw("Unexpected comma in object literal.", $pointer);
31
- }
32
- @return ($pointer, $map);
33
- }
34
-
35
- @else if $token == " " {
36
- // @continue;
37
- }
38
-
39
- @else if $token == "," {
40
- @if not $needs-comma {
41
- @return _throw("Unexpected comma in object literal.", $pointer);
42
- }
43
- $needs-comma: false;
44
- }
45
-
46
- @else if $token == '"' {
47
- @if $needs-comma {
48
- @return _throw("Missing comma in object literal.", $pointer);
49
- }
50
-
51
- // Read key
52
- $read-key: _json-decode--string($source, $pointer);
53
- $pointer: nth($read-key, 1);
54
- $key: nth($read-key, 2);
55
-
56
- // Remove colon
57
- $pointer: _consume($source, $pointer, ":");
58
- @if length($pointer) > 1 { // If consume has failed
59
- @return _throw("Consuming token `:` failed.", 0);
60
- }
61
-
62
- // Read value
63
- $read-value: _json-decode--value($source, $pointer);
64
- $pointer: nth($read-value, 1);
65
- $value: nth($read-value, 2);
66
-
67
- // Add pair to map
68
- $map: map-merge($map, ($key: $value));
69
- $needs-comma: true;
70
- }
71
-
72
- @else {
73
- @return _throw("Unexpected token `" + $token + "` in object literal.", $pointer);
74
- }
75
- }
76
-
77
- @return _throw("Unterminated object literal.", $pointer);
78
- }
1
+ // Parses a JSON encoded object
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $source: JSON complete source
4
+ // @param [number] $pointer: current pointer
5
+ // --------------------------------------------------------------------------------
6
+ // @throw "Unexpected comma in object literal."
7
+ // @throw "Unexpected token $token in object literal."
8
+ // @throw "Missing comma in object literal."
9
+ // @throw "Unterminated object literal."
10
+ // @throw "Consuming token `:` failed."
11
+ // --------------------------------------------------------------------------------
12
+ // @return [list|false] (new pointer, map)
13
+
14
+ @function _json-decode--map($source, $pointer) {
15
+ $length: str-length($source);
16
+ $map: ();
17
+ $needs-comma: false;
18
+
19
+ // Deal with empty map
20
+ @if $pointer <= $length and str-slice($source, $pointer, $pointer) == "}" {
21
+ @return ($pointer + 1, $map);
22
+ }
23
+
24
+ @while $pointer <= $length {
25
+ $token: str-slice($source, $pointer, $pointer);
26
+ $pointer: $pointer + 1;
27
+
28
+ @if $token == "}" {
29
+ @if not $needs-comma and length($map) != 0 {
30
+ @return _throw("Unexpected comma in object literal.", $pointer);
31
+ }
32
+ @return ($pointer, $map);
33
+ }
34
+
35
+ @else if $token == " " {
36
+ // @continue;
37
+ }
38
+
39
+ @else if $token == "," {
40
+ @if not $needs-comma {
41
+ @return _throw("Unexpected comma in object literal.", $pointer);
42
+ }
43
+ $needs-comma: false;
44
+ }
45
+
46
+ @else if $token == '"' {
47
+ @if $needs-comma {
48
+ @return _throw("Missing comma in object literal.", $pointer);
49
+ }
50
+
51
+ // Read key
52
+ $read-key: _json-decode--string($source, $pointer);
53
+ $pointer: nth($read-key, 1);
54
+ $key: nth($read-key, 2);
55
+
56
+ // Remove colon
57
+ $pointer: _consume($source, $pointer, ":");
58
+ @if length($pointer) > 1 { // If consume has failed
59
+ @return _throw("Consuming token `:` failed.", 0);
60
+ }
61
+
62
+ // Read value
63
+ $read-value: _json-decode--value($source, $pointer);
64
+ $pointer: nth($read-value, 1);
65
+ $value: nth($read-value, 2);
66
+
67
+ // Add pair to map
68
+ $map: map-merge($map, ($key: $value));
69
+ $needs-comma: true;
70
+ }
71
+
72
+ @else {
73
+ @return _throw("Unexpected token `" + $token + "` in object literal.", $pointer);
74
+ }
75
+ }
76
+
77
+ @return _throw("Unterminated object literal.", $pointer);
78
+ }
@@ -1,19 +1,19 @@
1
- // Parses a JSON encoded `null`
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $source: JSON complete source
4
- // @param [number] $pointer: current pointer
5
- // --------------------------------------------------------------------------------
6
- // @throw "Unexpected token `n`."
7
- // --------------------------------------------------------------------------------
8
- // @return [list|false] (new pointer, null)
9
-
10
- @function _json-decode--null($source, $pointer) {
11
- $length: str-length($source);
12
- @if $length - $pointer < 2
13
- or str-slice($source, $pointer, $pointer) != 'u'
14
- or str-slice($source, $pointer + 1, $pointer + 1) != 'l'
15
- or str-slice($source, $pointer + 2, $pointer + 2) != 'l' {
16
- @return _throw("Unexpected token: `n`.", $pointer);
17
- }
18
- @return ($pointer + 3, null);
19
- }
1
+ // Parses a JSON encoded `null`
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $source: JSON complete source
4
+ // @param [number] $pointer: current pointer
5
+ // --------------------------------------------------------------------------------
6
+ // @throw "Unexpected token `n`."
7
+ // --------------------------------------------------------------------------------
8
+ // @return [list|false] (new pointer, null)
9
+
10
+ @function _json-decode--null($source, $pointer) {
11
+ $length: str-length($source);
12
+ @if $length - $pointer < 2
13
+ or str-slice($source, $pointer, $pointer) != 'u'
14
+ or str-slice($source, $pointer + 1, $pointer + 1) != 'l'
15
+ or str-slice($source, $pointer + 2, $pointer + 2) != 'l' {
16
+ @return _throw("Unexpected token: `n`.", $pointer);
17
+ }
18
+ @return ($pointer + 3, null);
19
+ }
@@ -1,63 +1,63 @@
1
- // Parses a JSON encoded number
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $source: JSON complete source
4
- // @param [number] $pointer: current pointer
5
- // --------------------------------------------------------------------------------
6
- // @throw "Unexpected token $token."
7
- // @throw "Unexpected end of stream."
8
- // --------------------------------------------------------------------------------
9
- // @return [list|false] (new pointer, parsed number)
10
-
11
- @function _json-decode--number($source, $pointer) {
12
- $pointer: $pointer - 1; // Move back pointer to begininng of number
13
- $allowed: '-' '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'; // Allowed characted to start with
14
- $first: str-slice($source, $pointer, $pointer); // First character of the number
15
- $minus: $first == '-'; // Is it negative?
16
-
17
- // Early check for errors
18
- @if not index($allowed, $first) {
19
- @return _throw("Unexpected token `" + $first + "`.", $pointer);
20
- }
21
-
22
- // Find the integer part
23
- $find-integer: _find-integer($source, $pointer);
24
- $pointer: nth($find-integer, 1);
25
- $result: nth($find-integer, 2);
26
- @if not $result { // Error occured
27
- @return $find-integer;
28
- }
29
-
30
- // Find digits
31
- @if str-slice($source, $pointer, $pointer) == '.' {
32
- $find-digits: _find-digits($source, $pointer);
33
- $pointer: nth($find-digits, 1);
34
- $digits: nth($find-digits, 2);
35
-
36
- @if $digits == null { // Empty digits, throw error
37
- @return _throw("Unexpected end of stream.", $pointer);
38
- }
39
- @else if $digits == false { // Error occured, return it
40
- @return $find-digits;
41
- }
42
-
43
- $result: $result + $digits;
44
- }
45
-
46
- // Find exponent
47
- @if to-lower-case(str-slice($source, $pointer, $pointer)) == 'e' {
48
- $find-exponent: _find-exponent($source, $pointer);
49
- $pointer: nth($find-exponent, 1);
50
- $exponent: nth($find-exponent, 2);
51
-
52
- @if $exponent == null { // Empty exponent, throw error
53
- @return _throw("Unexpected end of stream.", $pointer);
54
- }
55
- @else if $exponent == false { // Error occured, return it
56
- @return $find-exponent;
57
- }
58
-
59
- $result: $result * _pow(10, $exponent);
60
- }
61
-
62
- @return ($pointer, if($minus, $result * -1, $result));
63
- }
1
+ // Parses a JSON encoded number
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $source: JSON complete source
4
+ // @param [number] $pointer: current pointer
5
+ // --------------------------------------------------------------------------------
6
+ // @throw "Unexpected token $token."
7
+ // @throw "Unexpected end of stream."
8
+ // --------------------------------------------------------------------------------
9
+ // @return [list|false] (new pointer, parsed number)
10
+
11
+ @function _json-decode--number($source, $pointer) {
12
+ $pointer: $pointer - 1; // Move back pointer to begininng of number
13
+ $allowed: '-' '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'; // Allowed characted to start with
14
+ $first: str-slice($source, $pointer, $pointer); // First character of the number
15
+ $minus: $first == '-'; // Is it negative?
16
+
17
+ // Early check for errors
18
+ @if not index($allowed, $first) {
19
+ @return _throw("Unexpected token `" + $first + "`.", $pointer);
20
+ }
21
+
22
+ // Find the integer part
23
+ $find-integer: _find-integer($source, $pointer);
24
+ $pointer: nth($find-integer, 1);
25
+ $result: nth($find-integer, 2);
26
+ @if not $result { // Error occured
27
+ @return $find-integer;
28
+ }
29
+
30
+ // Find digits
31
+ @if str-slice($source, $pointer, $pointer) == '.' {
32
+ $find-digits: _find-digits($source, $pointer);
33
+ $pointer: nth($find-digits, 1);
34
+ $digits: nth($find-digits, 2);
35
+
36
+ @if $digits == null { // Empty digits, throw error
37
+ @return _throw("Unexpected end of stream.", $pointer);
38
+ }
39
+ @else if $digits == false { // Error occured, return it
40
+ @return $find-digits;
41
+ }
42
+
43
+ $result: $result + $digits;
44
+ }
45
+
46
+ // Find exponent
47
+ @if to-lower-case(str-slice($source, $pointer, $pointer)) == 'e' {
48
+ $find-exponent: _find-exponent($source, $pointer);
49
+ $pointer: nth($find-exponent, 1);
50
+ $exponent: nth($find-exponent, 2);
51
+
52
+ @if $exponent == null { // Empty exponent, throw error
53
+ @return _throw("Unexpected end of stream.", $pointer);
54
+ }
55
+ @else if $exponent == false { // Error occured, return it
56
+ @return $find-exponent;
57
+ }
58
+
59
+ $result: $result * _pow(10, $exponent);
60
+ }
61
+
62
+ @return ($pointer, if($minus, $result * -1, $result));
63
+ }
@@ -1,41 +1,41 @@
1
- // Parses a JSON encoded string
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $source: JSON complete source
4
- // @param [number] $pointer: current pointer
5
- // --------------------------------------------------------------------------------
6
- // @throw "Unterminated string."
7
- // --------------------------------------------------------------------------------
8
- // @return [list|false] (new pointer, parsed string / color / length)
9
-
10
- @function _json-decode--string($source, $pointer) {
11
- // Check for the end of the string
12
- $temp: str-slice($source, $pointer);
13
- $end: _find-ending-quote($temp);
14
- $string: "";
15
-
16
- // If no end is found
17
- @if not $end or $end == 0 {
18
- @return _throw("Unterminated string.", $pointer);
19
- }
20
-
21
- // If string is not empty
22
- @else if $end > 1 {
23
- $string: str-slice($temp, 1, $end - 1);
24
-
25
- $cr: "
26
- ";
27
- $string: _strip-token($string, "\r", $cr);
28
- $string: _strip-token($string, "\n", $cr);
29
- $string: _strip-token($string, '\\\"', '"');
30
-
31
- // Test whether the string could be a CSS length
32
- $string: _length($string);
33
-
34
- // Test whether the string could be a CSS color
35
- @if type-of($string) == string {
36
- $string: _color($string);
37
- }
38
- }
39
-
40
- @return ($pointer + $end, $string);
41
- }
1
+ // Parses a JSON encoded string
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $source: JSON complete source
4
+ // @param [number] $pointer: current pointer
5
+ // --------------------------------------------------------------------------------
6
+ // @throw "Unterminated string."
7
+ // --------------------------------------------------------------------------------
8
+ // @return [list|false] (new pointer, parsed string / color / length)
9
+
10
+ @function _json-decode--string($source, $pointer) {
11
+ // Check for the end of the string
12
+ $temp: str-slice($source, $pointer);
13
+ $end: _find-ending-quote($temp);
14
+ $string: "";
15
+
16
+ // If no end is found
17
+ @if not $end or $end == 0 {
18
+ @return _throw("Unterminated string.", $pointer);
19
+ }
20
+
21
+ // If string is not empty
22
+ @else if $end > 1 {
23
+ $string: str-slice($temp, 1, $end - 1);
24
+
25
+ $cr: "
26
+ ";
27
+ $string: _strip-token($string, "\r", $cr);
28
+ $string: _strip-token($string, "\n", $cr);
29
+ $string: _strip-token($string, '\\\"', '"');
30
+
31
+ // Test whether the string could be a CSS length
32
+ $string: _length($string);
33
+
34
+ // Test whether the string could be a CSS color
35
+ @if type-of($string) == string {
36
+ $string: _color($string);
37
+ }
38
+ }
39
+
40
+ @return ($pointer + $end, $string);
41
+ }
@@ -1,17 +1,17 @@
1
- // Delay the encoding of ta literal to JSON
2
- // to a type-specific method
3
- // --------------------------------------------------------------------------------
4
- // @param $value: value to be stringified
5
- // --------------------------------------------------------------------------------
6
- // @throw "Unknown type for $value ({x})."
7
- // --------------------------------------------------------------------------------
8
- // @return [string|false] JSON encoded string
9
-
10
- @function json-encode($value) {
11
- $type: type-of($value);
12
- @if function_exists('_json-encode--#{$type}') {
13
- @return call('_json-encode--#{$type}', $value);
14
- }
15
- @warn "Unknown type for #{$value} (#{$type}).";
16
- @return false;
17
- }
1
+ // Delay the encoding of ta literal to JSON
2
+ // to a type-specific method
3
+ // --------------------------------------------------------------------------------
4
+ // @param $value: value to be stringified
5
+ // --------------------------------------------------------------------------------
6
+ // @throw "Unknown type for $value ({x})."
7
+ // --------------------------------------------------------------------------------
8
+ // @return [string|false] JSON encoded string
9
+
10
+ @function json-encode($value) {
11
+ $type: type-of($value);
12
+ @if function_exists('_json-encode--#{$type}') {
13
+ @return call('_json-encode--#{$type}', $value);
14
+ }
15
+ @warn "Unknown type for #{$value} (#{$type}).";
16
+ @return false;
17
+ }
@@ -1,17 +1,17 @@
1
- // Helpers
2
- @import "helpers/quote";
3
-
4
- // Type specific encoding functions
5
- @import "types/bool";
6
- @import "types/color";
7
- @import "types/list";
8
- @import "types/map";
9
- @import "types/number";
10
- @import "types/string";
11
- @import "types/null";
12
-
13
- // Public API
14
- @import "api/json";
15
-
16
- // Mixin to pass the string to the DOM
17
- @import "mixins/json";
1
+ // Helpers
2
+ @import "helpers/quote";
3
+
4
+ // Type specific encoding functions
5
+ @import "types/bool";
6
+ @import "types/color";
7
+ @import "types/list";
8
+ @import "types/map";
9
+ @import "types/number";
10
+ @import "types/string";
11
+ @import "types/null";
12
+
13
+ // Public API
14
+ @import "api/json";
15
+
16
+ // Mixin to pass the string to the DOM
17
+ @import "mixins/json";