pegjs - Using PEG.js for simple search/replace -


im trying understand how use peg.js simple search/replace in text. surely not intended use parser anyway im curious logic behind these kind of languages produce search/replace.

the problem im having is hard define positively complementary of definitions. example: imagine want search , replace syntax:

rule = (whatever_is_not_my_syntax* m:my_syntax)+ {replace m} word = [a-z0-9_]+ my_syntax = word "." word whatever_is_not_my_syntax = ??? 

it hard describe, positively, whatever_is_not_my_syntax in peg.js without partial collision (and consequent parser error) my_syntax, or @ least dont know how it, because negative parser functions on peg.js !expression , [^characters].

can me? appreciate book or bibliography, if exists, topic. thank in advance.

you don't have specify what's not in syntax. first try match syntax, have fallback else.

here rule list of patterns in syntax. when doesn't match, other match instead.

expr =     a:rule " " b:expr         {return [a].concat(b)}   / a:rule         {return [a]}   / a:other " " b:expr         {return [a].concat(b)}   / a:other         {return [a]}  word =     a:[a-z0-9_]+         {return a.join("")}  rule =     word "." word         {return "rule1"} // put replace logic here   / word ":" word         {return "rule2"} // put replace logic here  other =     word         {return "other"} 

you can try online: http://pegjs.org/online


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -