refactored Url.hx to URI.hx

This commit is contained in:
Leon van Kammen 2023-03-31 14:47:54 +02:00
parent 69df1be36e
commit d61539282c
7 changed files with 23 additions and 68 deletions

View file

@ -1,42 +0,0 @@
# Fragment (values)
| param | type | category | example |
|---------|---------------|-------------------------|------------------|
| pos | 3D vector | HREF navigation/portals | `#pos=1,0,1` or `#pos=foo` |
| prio | int (-10..1) | Asset linking | `#prio=-5` |
# Url parser (the gist of it)
1. fragment URI starts with `#`
1. fragments are split by `&`
1. fragment-values are urlencoded (space becomes `+` using `encodeUriComponent` e.g.)
1. `=` is used to split fragment key/values
1. `|` is used to split multiple/fallback values
1. `,` assumes 1D/2D/3D vector-values like x[,y[,z]]
1. parseFloat(..) and parseInt(..) is applied to vector/float and int values
1. anything else will be treated as string-value
1. incompatible value-types will be dropped / not used
> the xrfragment specification should stay simple enough
> for anyone to write a parser using either regexes or grammar/lexers
> therefore expressions/comprehensions are not supported (max wildcard/comparison operators for queries e.g.)
# Value types
| type | info | format | example |
|------|------|--------|----------------------------------|
|vector| x,y,z| comma-separated | #pos=1,2,3 |
|string| color| FFFFFF (hex) | #fog=5m,FFAACC |
|string| | | #q=-sun |
|int | | [-]x[xxxxx] | #price:>=100 |
|float | | [-]x[.xxxx] (ieee)| #prio=-20
|array | mixed| \|-separated | #pos=0,0,0|90,0,0 |
> rule for thumb: type-limitations will piggyback JSON limitations (IEEE floatsize e.g.)
1. hex colors are detected using regex `/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/`
1. integers are detected using regex `/^[0-9]+$/`
1. floats are detected using regex `/^[0-9]+\.[0-9]+$/`
1. vectors are detected using regex `/[,]/` (but can also be an string referring to an entity-ID in the asset)

View file

@ -1,10 +1,12 @@
> version 1.0.0
date: 2023-03-31T14:39:54+0200 (generated by `./make doc`)
date: 2023-03-31T14:47:04+0200 (generated by `./make doc`)
example URI: `://domain.com/some3d.asset#pos=1,0,0&prio=-5
# URI parser
> icanhazcode? yes, see [Url.hx](./../src/xrfragment/Url.hx)
> icanhazcode? yes, see [URI.hx](./../src/xrfragment/URI.hx)
1. fragment URI starts with `#`
1. fragments are split by `&`

2
make
View file

@ -45,7 +45,7 @@ doc(){
{
echo "> version $VERSION"
echo "\ndate: $(date +"%Y-%m-%dT%H:%M:%S%z") (generated by \`./make doc\`)"
generate src/xrfragment/Url.hx
generate src/xrfragment/URI.hx
generate src/xrfragment/Parser.hx
} > doc/RFC.md
}

View file

@ -1,5 +1,5 @@
import xrfragment.Query;
import xrfragment.Url;
import xrfragment.URI;
class Spec {
macro public static function load(path : String) {
@ -29,7 +29,7 @@ class Test {
var valid:Bool = false;
var item:Dynamic = spec[i];
if( item.fn == "query" ) q = new Query(item.data);
if( item.fn == "url" ) res = Url.parse(item.data);
if( item.fn == "url" ) res = URI.parse(item.data);
if( item.expect.fn == "test" ) valid = item.expect.out == q.test( item.expect.input[0] );
if( item.expect.fn == "testProperty" ) valid = item.expect.out == q.testProperty( item.expect.input[0], item.expect.input[1] );
if( item.expect.fn == "testPropertyExclude" ) valid = item.expect.out == q.testProperty( item.expect.input[0], item.expect.input[1], true );
@ -55,10 +55,10 @@ class Test {
}
static public function testUrl():Void {
var Url = xrfragment.Url;
var uri:String = "http://foo.com?foo=1#bar=flop&a=1,2&b=c|d|1,2,3";
trace(uri);
trace( Url.parse(uri) );
var Uri = xrfragment.URI;
var url:String = "http://foo.com?foo=1#bar=flop&a=1,2&b=c|d|1,2,3";
trace(url);
trace( Uri.parse(url) );
}
static public function testQuery():Void {

View file

@ -4,14 +4,12 @@ import xrfragment.Parser;
@:expose // <- makes the class reachable from plain JavaScript
@:keep // <- avoids accidental removal by dead code elimination
class Url {
public static var error:String = "";
//
// example URI: `://domain.com/some3d.asset#pos=1,0,0&prio=-5
class URI {
@:keep // # URI parser
public static function parse(qs:String):haxe.DynamicAccess<Dynamic> { //
var fragment:Array<String> = qs.split("#"); // > icanhazcode? yes, see [Url.hx](./../src/xrfragment/Url.hx)
var fragment:Array<String> = qs.split("#"); // > icanhazcode? yes, see [URI.hx](./../src/xrfragment/URI.hx)
var splitArray:Array<String> = fragment[1].split('&'); //
var resultMap:haxe.DynamicAccess<Dynamic> = {}; // 1. fragment URI starts with `#`
for (i in 0...splitArray.length) { // 1. fragments are split by `&`
@ -27,5 +25,4 @@ class Url {
}
return resultMap;
}
}

View file

@ -151,7 +151,7 @@ Test.test = function(spec) {
q = new xrfragment_Query(item.data);
}
if(item.fn == "url") {
res = xrfragment_Url.parse(item.data);
res = xrfragment_URI.parse(item.data);
}
if(item.expect.fn == "test") {
valid = item.expect.out == q.test(item.expect.input[0]);
@ -517,9 +517,9 @@ xrfragment_Query.prototype = {
return qualify > 0;
}
};
var xrfragment_Url = $hx_exports["xrfragment"]["Url"] = function() { };
xrfragment_Url.__name__ = true;
xrfragment_Url.parse = function(qs) {
var xrfragment_URI = $hx_exports["xrfragment"]["URI"] = function() { };
xrfragment_URI.__name__ = true;
xrfragment_URI.parse = function(qs) {
var fragment = qs.split("#");
var splitArray = fragment[1].split("&");
var resultMap = { };
@ -579,7 +579,6 @@ var xrfragment_Query_ok = $hx_exports["xrfragment"]["Query"]["ok"] =
}
}
;
xrfragment_Url.error = "";
Test.main();
})({});
var xrfragment = $hx_exports["xrfragment"];

View file

@ -400,7 +400,7 @@ class Test:
if (Reflect.field(item,"fn") == "query"):
q = xrfragment_Query(Reflect.field(item,"data"))
if (Reflect.field(item,"fn") == "url"):
res = xrfragment_Url.parse(Reflect.field(item,"data"))
res = xrfragment_URI.parse(Reflect.field(item,"data"))
if (Reflect.field(Reflect.field(item,"expect"),"fn") == "test"):
valid = (Reflect.field(Reflect.field(item,"expect"),"out") == q.test(HxOverrides.arrayGet(Reflect.field(Reflect.field(item,"expect"),"input"), 0)))
if (Reflect.field(Reflect.field(item,"expect"),"fn") == "testProperty"):
@ -1622,10 +1622,10 @@ class xrfragment_Query:
class xrfragment_Url:
_hx_class_name = "xrfragment.Url"
class xrfragment_URI:
_hx_class_name = "xrfragment.URI"
__slots__ = ()
_hx_statics = ["error", "parse"]
_hx_statics = ["parse"]
@staticmethod
def parse(qs):
@ -1661,6 +1661,5 @@ xrfragment_Type.isColor = EReg("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$","")
xrfragment_Type.isInt = EReg("^[0-9]+$","")
xrfragment_Type.isFloat = EReg("^[0-9]+\\.[0-9]+$","")
xrfragment_Type.isVector = EReg("([,]+|\\w)","")
xrfragment_Url.error = ""
Test.main()