mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
gfse: work in progress on support for example-based grammar writing
This commit is contained in:
@@ -492,7 +492,8 @@ function draw_concrete(g,i) {
|
||||
indent([kw("lincat"),draw_lincats(g,i)]),
|
||||
indent([kw("lin"),draw_lins(g,i)]),
|
||||
indent([extensible([kw("param"),draw_params(g,i)])]),
|
||||
indent([extensible([kw("oper"),draw_opers(g,i)])])
|
||||
indent([extensible([kw("oper"),draw_opers(g,i)])]),
|
||||
exb_extra(g,i)
|
||||
])
|
||||
}
|
||||
|
||||
@@ -780,8 +781,8 @@ function arg_names(type) {
|
||||
return map(unique,names);
|
||||
}
|
||||
|
||||
function draw_lins(g,i) {
|
||||
var conc=g.concretes[i];
|
||||
function draw_lins(g,ci) {
|
||||
var conc=g.concretes[ci];
|
||||
function edit(f) {
|
||||
return function(g,el) {
|
||||
function check(s,cont) {
|
||||
@@ -799,7 +800,7 @@ function draw_lins(g,i) {
|
||||
string_editor(el,f.lin,check,true)
|
||||
}
|
||||
}
|
||||
function del(fun) { return function () { delete_lin(g,i,fun); } }
|
||||
function del(fun) { return function () { delete_lin(g,ci,fun); } }
|
||||
function dl(f,cls) {
|
||||
var l=[ident(f.fun)]
|
||||
for(var i in f.args) {
|
||||
@@ -808,6 +809,7 @@ function draw_lins(g,i) {
|
||||
}
|
||||
l.push(sep(" = "));
|
||||
var t=editable("span",text_ne(f.lin),g,edit(f),"Edit lin for "+f.fun);
|
||||
appendChildren(t,exb_linbuttons(g,ci,f));
|
||||
l.push(t);
|
||||
return node("span",{"class":cls},l);
|
||||
}
|
||||
|
||||
147
src/editor/simple/example_based.js
Normal file
147
src/editor/simple/example_based.js
Normal file
@@ -0,0 +1,147 @@
|
||||
|
||||
var example_based=[];
|
||||
|
||||
/*
|
||||
-- cat lincat fun lin fun cat cat
|
||||
environ :: ([(CId, CId)],[(CId, Expr)],[((CId, CId), [CId])]) -> Environ
|
||||
*/
|
||||
function exb_state(g,ci) {
|
||||
var conc=g.concretes[ci]
|
||||
function show_list(show1,xs) {
|
||||
return "["+map(show1,xs).join(",")+"]";
|
||||
}
|
||||
function show_fun(fun) {
|
||||
var t=fun.type
|
||||
var res=t[t.length-1]
|
||||
var args=t.slice(0,length-1);
|
||||
return "(("+fun.name+","+res+"),["+args.join(",")+"])"
|
||||
}
|
||||
function show_lincat(lincat) {
|
||||
return "("+lincat.cat+","+lincat.type+")"
|
||||
}
|
||||
function show_lin(lin) {
|
||||
return "("+lin.fun+","+lin.lin+")"
|
||||
}
|
||||
function show_funs(funs) { return show_list(show_fun,funs) }
|
||||
function show_lincats(lincats) { return show_list(show_lincat,lincats); }
|
||||
function show_lins(lins) { return show_list(show_lin,lins) }
|
||||
return "("+show_lincats(conc.lincats)
|
||||
+","+show_lins(conc.lins)
|
||||
+","+show_funs(g.abstract.funs)+")"
|
||||
}
|
||||
|
||||
function exb_call(g,ci,command,args,cont) {
|
||||
var url="exb/exb.fcgi?command="+command+"&state="+exb_state(g,ci);
|
||||
for(var arg in args) url+="&"+arg+"="+encodeURIComponent(args[arg]);
|
||||
http_get_json(url,cont)
|
||||
}
|
||||
|
||||
function ask_possibilities(g,ci) {
|
||||
var conc=g.concretes[ci];
|
||||
|
||||
function show_poss(poss) {
|
||||
//debug("possibilities: "+JSON.stringify(poss))
|
||||
var exready={}
|
||||
for(var i in poss[0]) exready[poss[0][i]]=true;
|
||||
var testable={}
|
||||
for(var i in poss[1]) testable[poss[1][i]]=true;
|
||||
example_based[ci]={exready:exready,testable:testable}
|
||||
conc.example_based=true;
|
||||
conc.example_lang=g.concretes[0].langcode;
|
||||
reload_grammar(g);
|
||||
}
|
||||
|
||||
exb_call(g,ci,"possibilities",{},show_poss)
|
||||
}
|
||||
|
||||
function exb_extra(g,ci) {
|
||||
var conc=g.concretes[ci];
|
||||
function stop_exb() {
|
||||
conc.example_based=false;
|
||||
reload_grammar(g);
|
||||
}
|
||||
|
||||
function exblangmenu() {
|
||||
function opt(conc) { return option(conc.langcode,conc.langcode); }
|
||||
// skip target language
|
||||
var m =node("select",{},map(opt,g.concretes));
|
||||
m.onchange=function() { conc.example_lang=m.value }
|
||||
return m
|
||||
}
|
||||
|
||||
function ask_poss() { ask_possibilities(g,ci) }
|
||||
|
||||
if(navigator.onLine && conc.example_based && !example_based[ci]) ask_poss();
|
||||
return conc.langcode=="Eng"
|
||||
? indent([text("Example based editing: "),
|
||||
conc.example_based
|
||||
? node("span",{},[button("Stop",stop_exb),
|
||||
text(" Example language: "),
|
||||
exblangmenu()
|
||||
])
|
||||
: button("Start",ask_poss)])
|
||||
: text("")
|
||||
}
|
||||
|
||||
function exb_linbuttons(g,ci,f) {
|
||||
var conc=g.concretes[ci];
|
||||
var fun=f.fun;
|
||||
var eb=example_based[ci];
|
||||
var exb_output;
|
||||
function fill_example(tree) {
|
||||
exb_output.innerHTML="";
|
||||
if(f.template) conc.lins.push({fun:f.fun,args:f.args,lin:tree});
|
||||
else f.lin=s;
|
||||
ask_possibilities(g,ci)
|
||||
}
|
||||
function show_example(example){
|
||||
exb_output.innerHTML="";
|
||||
var s=prompt(example[1]);
|
||||
if(s) {
|
||||
var t=function_type(g,fun);
|
||||
var abscat=t[t.length-1]
|
||||
var cat=cat_lincat(conc,abscat)
|
||||
exb_output.innerHTML="...";
|
||||
//server.parse({from:"ParseEng",cat:cat,input:s},fill_example)
|
||||
exb_call(g,ci,"abstract_example",
|
||||
{cat:cat,input:s,abstract:example[0]},
|
||||
fill_example)
|
||||
}
|
||||
}
|
||||
function by_example() {
|
||||
var dir=local.get("dir")
|
||||
if(dir) {
|
||||
if(exb_output) {
|
||||
exb_output.innerHTML="...";
|
||||
exb_call(g,ci,"provide_example",
|
||||
{lang:g.basename+conc.example_lang,
|
||||
fun:fun,
|
||||
grammar:"."+dir+"/"+g.basename+".pgf"},
|
||||
show_example)
|
||||
}
|
||||
}
|
||||
else exb_output.innerHTML="Compile the grammar first!"
|
||||
}
|
||||
function show_test(txt) {
|
||||
exb_output.innerHTML="";
|
||||
exb_output.appendChild(text(txt))
|
||||
}
|
||||
function test_it(b) {
|
||||
if(exb_output) {
|
||||
exb_output.innerHTML="...";
|
||||
exb_call(g,ci,"test_function",{fun:fun},show_test)
|
||||
}
|
||||
}
|
||||
var buttons=[];
|
||||
if(conc.example_based && eb) {
|
||||
if(eb.exready[fun])
|
||||
buttons.push(button("By example",by_example))
|
||||
if(eb.testable[fun]) {
|
||||
var b=button("Test it",test_it);
|
||||
buttons.push(b)
|
||||
}
|
||||
var exb_output=empty("span");
|
||||
buttons.push(exb_output)
|
||||
}
|
||||
return buttons
|
||||
}
|
||||
@@ -14,6 +14,18 @@ function defined_funs(g) {
|
||||
return df;
|
||||
}
|
||||
|
||||
function function_type(g,fun) {
|
||||
with(g.abstract)
|
||||
for(var i in funs) if(funs[i].name==fun) return funs[i].type
|
||||
return null;
|
||||
}
|
||||
|
||||
function cat_lincat(conc,cat) {
|
||||
with(conc)
|
||||
for(var i in lincats) if(lincats[i].cat==cat) return lincats[i].type
|
||||
return null;
|
||||
}
|
||||
|
||||
function rename_category(g,oldcat,newcat) {
|
||||
function rename_cats(cats) {
|
||||
for(var i in cats) if(cats[i]==oldcat) cats[i]=newcat;
|
||||
|
||||
@@ -32,13 +32,14 @@ This page does not work without JavaScript.
|
||||
<hr>
|
||||
<div class=modtime><small>
|
||||
HTML
|
||||
<!-- hhmts start --> Last modified: Fri Jul 29 15:43:58 CEST 2011 <!-- hhmts end -->
|
||||
<!-- hhmts start --> Last modified: Tue Sep 27 15:41:36 CEST 2011 <!-- hhmts end -->
|
||||
</small></div>
|
||||
<a href="about.html">About</a>
|
||||
<pre id=debug></pre>
|
||||
<script type="text/javascript" src="support.js"></script>
|
||||
<script type="text/javascript" src="localstorage.js"></script>
|
||||
<script type="text/javascript" src="gf_abs.js"></script>
|
||||
<script type="text/javascript" src="example_based.js"></script>
|
||||
<script type="text/javascript" src="editor.js"></script>
|
||||
<script type="text/javascript" src="cloud.js"></script>
|
||||
<script type="text/javascript" src="sort.js"></script>
|
||||
|
||||
Reference in New Issue
Block a user