MediaWiki:Skin/Projekt:Adventure2/parser/raw: Unterschied zwischen den Versionen

aus Kamelopedia, der wüsten Enzyklopädie
Zur Navigation springen Zur Suche springen
(neue Features: multiple events; page-, item-deklarationen; kommentare; escaping.)
Zeile 5: Zeile 5:
 
   
 
   
 
  start
 
  start
   = Event
+
   = Declaration*
   
+
 
  Event
+
Declaration
 +
  = EventDec
 +
  / PageDec
 +
  / ItemDec
 +
 
 +
  /* EVENTS */
 +
 
 +
  EventDec
 
   = S? 'event' n:(S Identifier)? CC
 
   = S? 'event' n:(S Identifier)? CC
    sc:ScopeClause
 
 
     tr:TriggerClause
 
     tr:TriggerClause
 
     co:ConditionClause?
 
     co:ConditionClause?
 
     ac:ActionClause?
 
     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;})}; }
+
     End S? { return ["event", {name:n?n[1]:"<anonymous>", trigger:tr, condition:(co?co:function(){return true;}), action:(ac?ac:function(){return true;})}]; }
 
ScopeClause
 
  = 'in' S sc:Scope CC {return sc;}
 
 
   
 
   
 
  TriggerClause
 
  TriggerClause
Zeile 71: Zeile 74:
 
   / f:'print' L s:String R {return function(){a2.util.print(s)};}
 
   / f:'print' L s:String R {return function(){a2.util.print(s)};}
 
   / f:'die' L s:String R {return function(){a2.util.die(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
 
  L
Zeile 83: Zeile 118:
 
  CC
 
  CC
 
   = S? ';' S?
 
   = S? ';' S?
 
Scope
 
  = s:'page' S p:Page {return [s,p]; }
 
  / s:'item' S i:Item {return [s,i]; }
 
 
   
 
   
 
  Identifier
 
  Identifier
 
   = i1:[a-zäöü_] i2:[a-zäöü_]* {return i1+i2.join("");}
 
   = 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
 
  Page
 
   = p:[0-9]+ {return p.join("");}
 
   = p:[0-9]+ {return p.join("");}
Zeile 98: Zeile 135:
 
   
 
   
 
  S
 
  S
   = [ \t\n]+
+
   = [ \t\n]* "/*" [^*]* "*/" S?
 +
  / [ \t\n]* "//" [^\n]* [\n] S?
 +
  / [ \t\n]+
 
   
 
   
 
  String
 
  String
   = "'" s:[^']* "'" { return s.join("");}
+
   = "'" s:SingleStringChar* "'" { return s.join("");}
   / '"' s:[^"]* '"' { return s.join("");}
+
   / '"' s:DoubleStringChar* '"' { return s.join("");}
 +
 
 +
SingleStringChar
 +
  = EscapeSequence
 +
  / [^']
 +
 
 +
DoubleStringChar
 +
  = EscapeSequence
 +
  / [^"]
 +
 
 +
 
 +
EscapeSequence
 +
  = "\\n" {return "\n";}
 +
  / "\\\"" {return '"';}
 +
  / "\\\'" {return "'";}
 +
 
 +
End
 +
  = "end" S? "." S?

Version vom 10. Juli 2012, 17:33 Uhr

/*
 * A2 event syntax parser using pegjs
 * http://pegjs.majda.cz/online
 */

start
  = Declaration*
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]};}

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?