MediaWiki:Skin/Projekt:Adventure2/parser/raw

aus Kamelopedia, der wüsten Enzyklopädie
Version vom 28. April 2013, 10:29 Uhr von J* (Diskussion | Beiträge) (probieren wir's mal ...)
Zur Navigation springen Zur Suche springen
/*
 * A2 event syntax parser using pegjs
 * http://pegjs.majda.cz/online
 *
 * Parser variable: a2.parser
 * + Use results cache
 * + Track line and column
 */

start
  = S? d:Declaration* {return d;}
Declaration
  = EventDec
  / PageDec
  / ItemDec
/* EVENTS */
EventDec
  = S? 'event' n:(S Identifier)? CC
    tr:TriggerClause
    co:ConditionClause?
    ac:ActionClause?
    End S? { return ["event", {name:n?n[1]:":anonymous:", trigger:tr, condition:(co?co:function(){return true;}), action:(ac?ac:function(){return true;})}]; }

TriggerClause
  = 'on' S S? tr:TriggerFunctions CC {return tr;}

TriggerFunctions
  = t:TriggerFunction C o:TriggerFunctions {o.push(t); return o;}
  / t:TriggerFunction {return [t];}

TriggerFunction
  = t:TriggerPageOrVirtOp L a:(Page/PageVirtual) R {return t+"."+a;}
  / t:TriggerItemOrVirtOp L a:(Item/ItemVirtual) R {return t+"."+a;}
  / t:"click" L a:Identifier R {return t+"."+a;}
  / t:"combine" L a:(Item/ItemVirtual) C b:(Item/ItemVirtual) R {return [t,a,b].join(".")}
  / t:"apply" L a:(Item/ItemVirtual) C b:Identifier R {return [t,a,b].join(".")}

TriggerPageOrVirtOp
  = 'enter'
  / 'leave'
TriggerItemOrVirtOp
  = 'add'
  / 'remove'
  / 'activate'

ConditionClause
  = 'if' S co:ConditionFunction CC {return co;}

ConditionFunction
  = 'page' L p:Page R {return function(){return a2.cpage == p};}
  / 'any' L a:ConditionArgs R {return function(){for(var j=0; j<a.length; j++) if (a[j]()) return true; return false; }}
  / 'all' L a:ConditionArgs R {return function(){for(var j=0; j<a.length; j++) if (!a[j]()) return false; return true; }}
  / 'not' L f:ConditionFunction R {return function(){ return !f()};}
  / 'have' L i:Item R {return function(){return !!a2.items[i]};}
  / 'page' L v:PageVirtual R {return function(){return !!a2.util.hasVirtual(v);};}
  / 'have' L v:ItemVirtual R {return function(){return !!a2.util.hasVirtual(v);};}

ConditionArgs
  = f:ConditionFunction C a:ConditionArgs {a.push(f); return a;}
  / f:ConditionFunction {return [f];}

ActionClause
  = 'do' S ac:ActionFunctions CC {return ac;}

ActionFunctions
  = f:ActionFunction C a:ActionFunctions {return function(real){f(real); a(real);};}
  / f:ActionFunction C "setTimeout" L t:Time R C a:ActionFunctions {return function(real){f(real); a2.timer.set(t, function(){a(real)});};}
  / ActionFunction 

ActionFunction
  = f:'goto' L p:Page R {return function(real){a2.util.goto(p)};}
  / f:'add' L i:Item R {return function(real){a2.util.add(i)};}
  / f:'remove' L i:Item R {return function(real){a2.util.remove(i)};}
  / f:'print' L s:String R {return function(real){a2.util.print(s)};}
  / f:'clearTimeout' L R {return function(real){a2.timer.clear();};}
  / f:'remove' L i:ItemVirtual R {return function(real){a2.util.removeAll(i)};}
  / f:'remove' L '${' rt:(TriggerItemOrVirtOp / "combine" / "apply") rn:[12] '}' R {return function(real){if (rt == real[0] && a2.virtualTriggerMap[rt] && a2.virtualTriggerMap[rt][parseInt(rn)-1]) a2.util.remove(real[parseInt(rn)]);};}
/* PAGES AND ITEMS */
PageDec
  = "page" CC
    pv:PageVirtualClause?
    End {return ["page", {virt:pv}];}
PageVirtualClause
  = "provides" S v:PageVirtualList CC {return v;}
PageVirtualList
  = v:PageVirtual C vl:PageVirtualList {vl.push(v); return vl;}
  / v:PageVirtual {return [v];}
ItemDec
  = "item" CC
    t:TitleClause?
    iv:ItemVirtualClause?
    End {return ["item", {virt:iv, title:t}];}
TitleClause
  = "title" S s:String CC {return s;}
ItemVirtualClause
  = "provides" S v:ItemVirtualList CC {return v;}
ItemVirtualList
  = v:ItemVirtual C vl:ItemVirtualList {vl.push(v); return vl;}
  / v:ItemVirtual {return [v];}
/* UTILITIES */

L
  = S? '(' S?

R
  = S? ')' S?

C
  = S? ',' S?

CC
  = S? ';' S?

Identifier
  = i1:[a-zäöü_] i2:[a-zäöü_]* {return i1+i2.join("");}

PageVirtual
  = p:"@" i:Identifier {return p+i;}
ItemVirtual
  = p:":" i:Identifier {return p+i;}
Page
  = p:[0-9]+ {return p.join("");}
Time
  = p:[1-9] q:[0-9]* {return parseInt(p+q.join(""));}

Item
  = Identifier

S
  = [ \t\n]* "/*" ([^*] / [*][^/] )* "*/" S?
  / [ \t\n]* "//" [^\n]* [\n] S?
  / [ \t\n]+

String
  = "'" s:SingleStringChar* "'" { return s.join("");}
  / '"' s:DoubleStringChar* '"' { return s.join("");}
SingleStringChar
  = EscapeSequence
  / [^']
DoubleStringChar
  = EscapeSequence
  / [^"]


EscapeSequence
  = "\\n" {return "\n";}
  / "\\\"" {return '"';}
  / "\\\'" {return "'";}
End
  = "end" S? "." S?