经过进一步搜索,我碰到了rexical的宝石,它本身就是雷克斯的重命名和伟略维护版本。这是一个老派的词法分析器生成器,它只依赖于racc/parser,它已经成为ruby-core的一部分,我不必担心它。
文档很少,但有足够的博客文章涉及到我能够获得我需要的工作。
如果你足够自信已经读到这里,这里是我的榜样.rex规格:
require 'generator'
class OptionSpecsLexer
rules
\d+(\.\d*) { [:number, text] }
\w+: { [:syntax_hash_key, ":#{text[0, text.length - 1]} =>"] }
\:\w+ { [:symbol, text] }
\w+\( { [:funcall_open_paren, text] }
\w+ { [:identifier, text] }
\"(\\.|[^\\"])*\" { [:string, text] }
=> { [:rocket, text] }
, { [:comma, text] }
\{ { [:open_curly, text] }
\} { [:close_curly, text] }
\( { [:open_paren, text] }
\) { [:close_paren, text] }
\[ { [:close_square, text] }
\] { [:close_square, text] }
\\\s+ { }
\n { [:eol, text] }
\s+ { }
inner
def enumerate_tokens
Generator.new { |token|
loop {
t = next_token
break if t.nil?
token.yield(t)
}
}
end
def normalize(source)
scan_setup source
out = ""
enumerate_tokens.each do |token|
out += ' ' + token[1]
end
out
end
end
这词法理解刚够红宝石语法预处理规范写在我的vMATCodeMonkey DSL,用旧的火箭运营商语法替换新的关键字样式的散列键语法。 [这样做是为了让vMATCodeMonkey对未更新的的Mac OS X 10.8工作仍然附带红宝石的一个过时版本。]