Wanna use JSON syntax in Felix? Here we go:

(1) Write the grammar

// lib/grammar/json.flxh

syntax json {
  requires expressions;
  x[sapplication_pri] := "json" "(" json_value ")" =># "_3";

  json_object : string = "{" "}";
  json_object : string = "{" json_members "}";
  json_members : string = json_pair ( "," json_pair ) *;
  json_pair : string = strstring ":" json_value;

  json_array : string = "[" "]";
  json_array : string = "[" json_elements "]";
  json_elements : string = json_value ( "," json_value ) *;

  json_value : string = 
     | strstring 
     | json_number
     | json_object
     | json_array
     | "true"
     | "false"
     | "null"
  ;
  json_number : string = 
     | strint
     | strfloat
  ;
}

(2) add the file to the files list:

// lib/grammar/grammar.files
grammar/grammar_scheme_support.flx
grammar/expressions.flxh
grammar/texsyms.flxh
grammar/patterns.flxh
grammar/functions.flxh
grammar/statements.flxh
grammar/variables.flxh
grammar/macros.flxh
grammar/cbind.flxh
grammar/executable.flxh
grammar/assignment.flxh
grammar/control.flxh
grammar/conditional.flxh
grammar/loops.flxh
grammar/requirements.flxh
grammar/type_decls.flxh
grammar/assertions.flxh
grammar/namespaces.flxh
grammar/regexps.flxh
grammar/cgram.flxh
grammar/json.flxh                <-----
grammar/felix.flxh
grammar/save.flxh

(3) Make it part of Felix:

syntax felix {
  requires
    statements,
    type_decls,
    variables,
    executable,
    assignment,
    control,
    conditional,
    loops,
    assertions,
    namespaces,
    requirements,
    expressions,
    texsyms,
    functions,
    patterns,
    cbind,
    regexps,
    macros,
    json            <----------
  ;
}

(4) delete lib/grammar/syntax.automaton

(5) Run a test program:

// jt.flx
val x = json ( [true, 12_3.4, 'Hello', {"HI": 1, "LO":2}, null] );
println$ x;

~/felix>flx --test=build/release --force jt
Deprecated Scheme string [ true , 123.4 , "Hello" , { "HI" : 1 , "LO" : 2 } , 
null ]' as Felix string
[ true , 123.4 , "Hello" , { "HI" : 1 , "LO" : 2 } , null ]


(6) Ignore the "Deprecated Scheme string thing .. small bug to fix .. 

(7) Time to party!


It's that easy. String grammars translate syntax into strings. The result is 
just
an ordinary string. But it is one which is syntax checked by the Felix parser.

Note that by luck, Felix numbers and strings can be used, but the representation
is normalised to something which, by luck, is close to what JSON actually 
requires.

--
john skaller
skal...@users.sourceforge.net





------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to