扩展解析器以构建模式的准引用

时间:2019-02-09 01:33:01

标签: haskell parsec template-haskell quasiquotes

我想构建一个准引用程序来生成与Java AST中的子树匹配的模式,以便代替function walk (node) { if (node.nodeType === 3) { handleText (node); return; } Array.prototype.forEach.call( node.childNodes, (childNode) => { const { tagName } = childNode; if (tagName !== 'SCRIPT' && tagName !== 'STYLE') { walk (child); } } ); } 我可以写类似import time, sched time = 1549667056000 # starting timestamp def get_data(): # function to get some data from the API via requests #gets the Data while true: s.scheduler(time) s.run(get_data()) # gets the data on the specified time(stamp) time = time + 60000 # adds 1 minute to the timestamp 的东西。一个幼稚的方法是修改Java语言解析器中的每个Parsec规则,例如:

BasicFor (Just (ForInitExps init)) (Just cond) (Just inc) body

成为

[java|for (#init ; #cond ; #inc) #body|]

我在哪里1.更改类型签名以生成catch :: P Catch catch = do tok KW_Catch fp <- parens formalParam b <- block return $ Catch fp b ; 2.添加备用catchP :: P TH.Pat catchP = bindP <|> do tok KW_Catch fp <- parens formalParam b <- block return $ ConP "Catch" [fp, b] 来解析元变量(例如上述TH.Pat); 3.将返回表达式修改为bindP而不是#init。对于每个单独的parsec规则,我都必须这样做,这是很多样板代码。有没有办法报废这种样板?也许使用ParsecT的底层monad,还是侵入parsec本身?

0 个答案:

没有答案