11package xrfragment ;
22
3+ /**
4+ * # XR Fragments (key/value params)
5+ *
6+ * > ⛁ = define in 3D asset-file (as custom property or default projection)<br>
7+ * > ☇ = mutable, using navigator URI (`document.location.href` e.g.)<br>
8+ */
9+
310@:expose // <- makes the class reachable from plain JavaScript
411@:keep // <- avoids accidental removal by dead code elimination
5- class Parser { // # XR Fragments (key/value params)
6- public static var error : String = " " ; //
7- // > ⛁ = define in 3D asset-file (as custom property or default projection)<br>
8- // > ☇ = mutable, using navigator URI (`document.location.href` e.g.)<br>
9- @:keep //
12+ class Parser {
13+ public static var error : String = " " ;
14+
15+ @:keep
1016 public static function parse (key : String ,value : String ,resultMap : haxe. DynamicAccess <Dynamic >): Bool {
1117 var Frag : Map <String , EReg > = new Map <String , EReg >(); // | param | type | scope(s) | category | notes |
1218 // |---------|---------------|-------|--------------------|---------------------------------|
13- Frag .set (" prio" , Type .isInt ); // | prio | int (-10..1) | ⛁ | Asset loading / linking | #include doc/notes/prio.md |
19+ Frag .set (" prio" , Type .isInt ); // | prio | int (-10..1) | ⛁ | Asset loading / linking | $(cat doc/notes/prio.md) |
1420
1521 Frag .set (" pos" , Type .isVector ); // | pos | 3D vector | ⛁ ☇ |HREF navigation/portals | |
1622 Frag .set (" q" , Type .isString ); // | q | string | ⛁ |Query Selector | |
17- //
18- // # XR Fragments parser
19- if ( Frag .exists (key ) ){ // note: community parsers will prolly outperform this initial parser :)
20- if ( Frag .get (key ).match (value ) ){ // > icanhazcode? yes, see [Parser.hx](https://github.com/coderofsalvation/xrfragment/blob/main/src/xrfragment/Parser.hx)
21- var v : Value = new Value (); //
22- guessType (v , value ); // the gist of it:
23- // process multiple/fallback values // 1. each key has a regex to validate its value-type (see regexes)
24- if ( value .split (" |" ).length > 1 ){ // 1. `|` is used to split multiple/fallback values
23+ var vec : String = " 1,2,3" ; //
24+ // if( Type.isVector(vec) ) trace("ja");
25+ /**
26+ * # XR Fragments parser
27+ *
28+ * note: community parsers will prolly outperform this initial parser :)
29+ * > icanhazcode? yes, see [Parser.hx](https://github.com/coderofsalvation/xrfragment/blob/main/src/xrfragment/Parser.hx)
30+ * the gist of it:
31+ */
32+ if ( Frag .exists (key ) ){ // 1. check if param exist
33+ if ( Frag .get (key ).match (value ) ){ // 1. each key has a regex to validate its value-type (see regexes)
34+ var v : Value = new Value ();
35+ guessType (v , value ); // 1. extract the type
36+ // process multiple/fallback values
37+ if ( value .split (" |" ).length > 1 ){ // 1. use `|` on stringvalues, to split multiple/fallback values
2538 v .args = new Array <Value >();
2639 var args : Array <String > = value .split (" |" );
2740 for ( i in 0 ... args .length ){
2841 var x : Value = new Value ();
29- guessType (x , args [i ]);
42+ guessType (x , args [i ]); // 1. for each multiple/fallback value, guess the type
3043 v .args .push ( x );
3144 }
3245 }
@@ -40,14 +53,14 @@ class Parser { // #
4053 @:keep
4154 public static function guessType (v : Value , str : String ): Void {
4255 v .string = str ;
43- if ( str .split (" ," ).length > 1 ){ // 1. `,` assumes 1D/2D/3D vector-values like x[,y[,z]]
44- var xyz : Array <String > = str .split (" ," ); // 1. parseFloat(..) and parseInt(..) is applied to vector/float and int values
45- if ( xyz .length > 0 ) v .x = Std .parseFloat (xyz [0 ]); // 1. anything else will be treated as string-value
46- if ( xyz .length > 1 ) v .y = Std .parseFloat (xyz [1 ]); // 1. incompatible value-types will be dropped / not used
47- if ( xyz .length > 2 ) v .y = Std .parseFloat (xyz [2 ]); //
48- } // > the xrfragment specification should stay simple enough
49- // > for anyone to write a parser using either regexes or grammar/lexers
50- if ( Type .isColor .match (str ) ) v .color = str ; // > therefore expressions/comprehensions are not supported (max wildcard/comparison operators for queries e.g.)
56+ if ( str .split (" ," ).length > 1 ){ // / 1. `,` assumes 1D/2D/3D vector-values like x[,y[,z]]
57+ var xyz : Array <String > = str .split (" ," ); // / 1. parseFloat(..) and parseInt(..) is applied to vector/float and int values
58+ if ( xyz .length > 0 ) v .x = Std .parseFloat (xyz [0 ]); // / 1. anything else will be treated as string-value
59+ if ( xyz .length > 1 ) v .y = Std .parseFloat (xyz [1 ]); // / 1. incompatible value-types will be dropped / not used
60+ if ( xyz .length > 2 ) v .y = Std .parseFloat (xyz [2 ]); // /
61+ } / / / > the xrfragment specification should stay simple enough
62+ // / > for anyone to write a parser using either regexes or grammar/lexers
63+ if ( Type .isColor .match (str ) ) v .color = str ; // / > therefore expressions/comprehensions are not supported (max wildcard/comparison operators for queries e.g.)
5164 if ( Type .isFloat .match (str ) ) v .float = Std .parseFloat (str );
5265 if ( Type .isInt .match (str ) ) v .int = Std .parseInt (str );
5366 }
@@ -58,8 +71,8 @@ class Parser { // #
5871 // | type | info | format | example |
5972class Value { // |------|------|--------|----------------------------------|
6073 public var x : Float ; // |vector| x,y,z| comma-separated | #pos=1,2,3 |
61- public var y : Float ; //
62- public var z : Float ; //
74+ public var y : Float ;
75+ public var z : Float ;
6376 public var color : String ; // |string| color| FFFFFF (hex) | #fog=5m,FFAACC |
6477 public var string : String ; // |string| | | #q=-sun |
6578 public var int : Int ; // |int | | [-]x[xxxxx] | #price:>=100 |
@@ -68,7 +81,7 @@ class Value { // |
6881 public function new (){} //
6982 // > rule for thumb: type-limitations will piggyback JSON limitations (IEEE floatsize e.g.)
7083}
71- // Regexes:
84+ // Regexes:
7285class Type { //
7386 static public var isColor : EReg = ~/ ^ #([A-Fa-f0-9 ] {6} | [A-Fa-f0-9 ] {3} )$ / ; // 1. hex colors are detected using regex `/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/`
7487 static public var isInt : EReg = ~/ ^ [0-9 ] + $ / ; // 1. integers are detected using regex `/^[0-9]+$/`
@@ -77,6 +90,6 @@ class Type { //
7790 static public var isString : EReg = ~/ . * / ; // 1. anything else is string `/.*/`
7891}
7992
80- // # Tests
81- //
82- // the spec is tested with [JSON unittests](./../src/spec) consumed by [Test.hx](./../src/Test.hx) to cross-test all languages.
93+ // / # Tests
94+ // /
95+ // / the spec is tested with [JSON unittests](./../src/spec) consumed by [Test.hx](./../src/Test.hx) to cross-test all languages.
0 commit comments