MediaWiki:Skin/Projekt:Adventure2/parser/raw

aus Kamelopedia, der wüsten Enzyklopädie
Zur Navigation springen Zur Suche springen
/*
 * A2 event syntax parser using pegjs
 * http://pegjs.majda.cz/online
 */

start
  = Event

Event
  = S? 'event' n:(S Identifier)? CC
    sc:ScopeClause
    tr:TriggerClause
    co:ConditionClause?
    ac:ActionClause?
    'end.' S? { return {name:n?n[1]:"<anonymous>", scope:sc, trigger:tr, condition:(co?co:function(){return true;}), action:(ac?ac:function(){return true;})}; }

ScopeClause
  = 'in' S sc:Scope CC {return sc;}

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:TriggerPageOp L a:Page R {return t+"."+a;}
  / t:TriggerIdentOp L a:Identifier R {return t+"."+a;}
  / t:TriggerBinaryOp L a:Identifier C b:Identifier R {return [t,a,b].join(".")}

TriggerPageOp
  = 'enter'
  / 'leave'

TriggerIdentOp
  = 'add'
  / 'remove'
  / 'activate'
  / 'click'
TriggerBinaryOp
  = 'combine'
  / 'apply'

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

ConditionFunction
  = 'page' L p:Page R {return function(state){return state.cpage == p};}
  / 'any' L a:ConditionArgs R {return function(state){for(var j=0; j<a.length; j++) if (a[j](state)) return true; return false; }}
  / 'have' L i:Item R {return function(state){return state.citems.indexOf(i) > -1};}

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(action){a(); f();};}
  / ActionFunction 

ActionFunction
  = f:'goto' L p:Page R {return function(action){a2.util.goto(p)};}
  / f:'add' L i:Item R {return function(action){a2.util.add(i)};}
  / f:'remove' L i:Item R {return function(action){a2.util.remove(i)};}
  / f:'print' L s:String R {return function(action){a2.util.print(s)};}
  / f:'die' L s:String R {return function(action){a2.util.die(s)};}

L
  = S? '(' S?

R
  = S? ')' S?

C
  = S? ',' S?

CC
  = S? ';' S?

Scope
  = s:'page' S p:Page {return [s,p]; } 
  / s:'item' S i:Item {return [s,i]; }

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

Page
  = p:[0-9]+ {return p.join("");}

Item
  = Identifier

S
  = [ \t\n]+

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