MediaWiki:Skin/Projekt:Adventure2/parser/raw

aus Kamelopedia, der wüsten Enzyklopädie
Version vom 14. Juli 2012, 10:36 Uhr von J* (Diskussion | Beiträge) (vielleicht gehts so wieder)
Zur Navigation springen Zur Suche springen
/*
 * A2 event syntax parser using pegjs
 * http://pegjs.majda.cz/online
 */

start
  = Declaration+
  / S+ {return [];}
  / "" {return [];}
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: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(){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.hasVirtual(v);};}
  / 'have' L v:ItemVirtual R {return function(){return !!a2.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(action){a(); f();};}
  / ActionFunction 

ActionFunction
  = f:'goto' L p:Page R {return function(){a2.util.goto(p)};}
  / f:'add' L i:Item R {return function(){a2.util.add(i)};}
  / f:'remove' L i:Item R {return function(){a2.util.remove(i)};}
  / f:'print' L s:String R {return function(){a2.util.print(s)};}
  / f:'die' L s:String R {return function(){a2.util.die(s)};}
/* 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("");}

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?