我想构建一个准引用程序来生成与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本身?