[英]how to parse multiple lines using ruby treetop?
我是ruby
和treetop
新手。
我完成了本教程,並提出了以下規則。
grammar Sexp
rule body
commentPortString *(I am stuck here)*
end
rule interface
space? (intf / intfWithSize) space? ('\n' / end_of_file) <Interface>
end
rule commentPortString
space? '//' space portString space? ('\n' / end_of_file) <CommentPortString>
end
rule portString
'Port' space? '.' <PortString>
end
rule expression
space? '(' body ')' space? <Expression>
end
rule intf
(input / output) space wire:wireName space? ';' <Intf>
end
rule intfWithSize
(input / output) space? width:ifWidth space? wire:wireName space? ';' <IntfWithSize>
end
rule input
'input'
end
rule output
'output'
end
rule ifWidth
'[' space? msb:digits space? ':' space? lsb:digits ']' <IfWidth>
end
rule digits
[0-9]+
end
rule integer
('+' / '-')? [0-9]+ <IntegerLiteral>
end
rule float
('+' / '-')? [0-9]+ (('.' [0-9]+) / ('e' [0-9]+)) <FloatLiteral>
end
rule string
'"' ('\"' / !'"' .)* '"' <StringLiteral>
end
rule signalTypeString
'"' if_sig_name:signalType '"' <SignalTypeString>
end
rule signalType
[a-zA-Z] [a-zA-Z0-9_]* (receiveLiteral / transmitLiteral) <SignalType>
end
rule receiveLiteral
'.receive'
end
rule transmitLiteral
'.transmit'
end
rule identifier
[a-zA-Z\=\*] [a-zA-Z0-9_\=\*]* <Identifier>
end
rule wireName
[a-zA-Z] [a-zA-Z0-9_]* <WireName>
end
rule non_space
!space .
end
rule space
[\s\t]+
end
rule newLine
[\n\r]+
end
rule end_of_file
!.
end
end
我希望解析器提取出下面的blob。 它總是以Port.
開頭Port.
並以空白行結尾。
// Port.
output send;
input free;
output fgcg;
output[ 2:0] state_id;
output[ 1:0] stream_id;
`ifdef SIMULATION
output[ 83:0] dbg_id;
`endif
上面提到的規則在單獨傳遞時可以識別文本中的所有行,但是我無法提取出斑點。 我也只想提取匹配的文本,而忽略其余文本。
有人可以指出我正確的方向。
類似於您要查找的內容。 如果沒有更多信息,很難完全理解您的問題。
space
規則包含\\s
,其中已經包含\\n
,因此,如果您要查找另一個\\n
,它將無法正確解析。 如果將space
規則修改為[^\\S\\n]+
,它將排除\\n
因此您可以明確查找它。
如果您要尋找一個完全空白的行來結束Port.
塊,您應該顯式查找"\\n" ("\\n" / end_of_file)
。
希望有道理...
grammar Sexp
rule body
commentPortString interface* portEnd
end
rule interface
space? (intf / intfWithSize) space? "\n" <Interface>
end
rule commentPortString
space? '//' space? portString space? "\n" <CommentPortString>
end
rule portString
'Port' space? '.' <PortString>
end
# Port block ends with a blank line
rule portEnd
"\n" / end_of_file
end
rule expression
space? '(' body ')' space? <Expression>
end
rule intf
(input / output) space wire:wireName space? ';' <Intf>
end
rule intfWithSize
(input / output) space? width:ifWidth space? wire:wireName space? ';' <IntfWithSize>
end
rule input
'input'
end
rule output
'output'
end
rule ifWidth
'[' space? msb:digits space? ':' space? lsb:digits ']' <IfWidth>
end
rule digits
[0-9]+
end
rule integer
('+' / '-')? [0-9]+ <IntegerLiteral>
end
rule float
('+' / '-')? [0-9]+ (('.' [0-9]+) / ('e' [0-9]+)) <FloatLiteral>
end
rule string
'"' ('\"' / !'"' .)* '"' <StringLiteral>
end
rule signalTypeString
'"' if_sig_name:signalType '"' <SignalTypeString>
end
rule signalType
[a-zA-Z] [a-zA-Z0-9_]* (receiveLiteral / transmitLiteral) <SignalType>
end
rule receiveLiteral
'.receive'
end
rule transmitLiteral
'.transmit'
end
rule identifier
[a-zA-Z\=\*] [a-zA-Z0-9_\=\*]* <Identifier>
end
rule wireName
[a-zA-Z] [a-zA-Z0-9_]* <WireName>
end
rule non_space
!space .
end
rule space
[^\S\n]+
end
rule newLine
[\n\r]+
end
rule end_of_file
!.
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.