Add count on repeat when at least one occurence is required

develop
Guillaume DOTT 2013-10-03 11:14:28 +02:00
parent 3656fd6d82
commit 926d7ec544
4 changed files with 18 additions and 14 deletions

View File

@ -26,11 +26,11 @@ module Biju
rule(:mgl) do rule(:mgl) do
(str('+CMGL').as(:cmd) >> str(': ') >> infos >> crlf >> message >> crlf) (str('+CMGL').as(:cmd) >> str(': ') >> infos >> crlf >> message >> crlf)
.repeat.as(:sms) >> crlf .repeat(1).as(:sms) >> crlf
end end
rule(:num) do rule(:num) do
(str('+CNUM').as(:cmd) >> str(': ') >> array >> crlf) (str('+CNUM').as(:cmd) >> str(': ') >> array >> crlf)
.repeat.as(:phone_numbers) >> crlf >> crlf .repeat(1).as(:phone_numbers) >> crlf >> crlf
end end
rule(:mgf) do rule(:mgf) do
str('+CMGF').as(:cmd) >> str(': ') >> boolean.as(:result) >> crlf >> crlf str('+CMGF').as(:cmd) >> str(': ') >> boolean.as(:result) >> crlf >> crlf
@ -42,7 +42,7 @@ module Biju
str('+CMGS').as(:cmd) >> str(': ') >> int.as(:result) >> crlf >> crlf str('+CMGS').as(:cmd) >> str(': ') >> int.as(:result) >> crlf >> crlf
end end
rule(:generic_response) do rule(:generic_response) do
match('[^:]').repeat.as(:cmd) >> str(': ') >> array >> match('[^:]').repeat(1).as(:cmd) >> str(': ') >> array >>
crlf >> crlf crlf >> crlf
end end
@ -52,7 +52,7 @@ module Biju
rule(:data) { (str('(') >> array >> str(')')) | info } rule(:data) { (str('(') >> array >> str(')')) | info }
rule(:infos) { (info >> (comma >> info).repeat).as(:infos) } rule(:infos) { (info >> (comma >> info).repeat).as(:infos) }
rule(:info) { datetime | string | int | empty_string } rule(:info) { datetime | string | int | empty_string }
rule(:message) { match('[0-9A-Fa-f]').repeat.as(:message) } rule(:message) { match('[0-9A-Fa-f]').repeat(1).as(:message) }
# MISC # MISC
rule(:status) { (ok | error).as(:status) } rule(:status) { (ok | error).as(:status) }
@ -84,20 +84,22 @@ module Biju
class ATTransform < Parslet::Transform class ATTransform < Parslet::Transform
rule(prompt: simple(:prompt)) { { prompt: true } } rule(prompt: simple(:prompt)) { { prompt: true } }
rule(cmd: simple(:cmd), infos: subtree(:infos), message: simple(:message)) do rule(cmd: simple(:cmd), infos: subtree(:infos), message: simple(:message)) do
{cmd: cmd.to_s, infos: infos, message: message.to_s} { cmd: cmd.to_s, infos: infos, message: message.to_s }
end end
rule(cmd: simple(:cmd), array: subtree(:array)) do rule(cmd: simple(:cmd), array: subtree(:array)) do
{cmd: cmd.to_s, array: array} { cmd: cmd.to_s, array: array }
end end
rule(cmd: simple(:cmd), result: simple(:result)) do rule(cmd: simple(:cmd), result: simple(:result)) do
{cmd: cmd.to_s, result: result} { cmd: cmd.to_s, result: result }
end end
rule(empty_string: simple(:empty_string)) { '' } rule(empty_string: simple(:empty_string)) { '' }
rule(int: simple(:int)) { int.to_i } rule(int: simple(:int)) { int.to_i }
rule(boolean: simple(:boolean)) { boolean.to_i > 0 } rule(boolean: simple(:boolean)) { boolean.to_i > 0 }
rule(string: simple(:string)) { string.to_s } rule(string: simple(:string)) { string.to_s }
rule(datetime: simple(:datetime)) { DateTime.strptime(datetime.to_s, "%y/%m/%d,%T%Z") } rule(datetime: simple(:datetime)) do
DateTime.strptime(datetime.to_s, '%y/%m/%d,%T%Z')
end
rule(array: subtree(:array)) { array } rule(array: subtree(:array)) { array }
rule(status: simple(:status)) { { status: status } } rule(status: simple(:status)) { { status: status } }

View File

@ -24,13 +24,13 @@ module Biju
'%02x' % first_octet.binary, '%02x' % first_octet.binary,
# TP-Message-Reference # TP-Message-Reference
'00', '00',
"%02x" % phone_number.length, '%02x' % phone_number.length,
"%02x" % phone_number.type_of_address.hex, '%02x' % phone_number.type_of_address.hex,
phone_number.number, phone_number.number,
# TP-PID: Protocol identifier # TP-PID: Protocol identifier
'00', '00',
"%02x" % user_data.encoding.hex, '%02x' % user_data.encoding.hex,
"%02x" % user_data.length, '%02x' % user_data.length,
user_data.message user_data.message
].join ].join
end end

View File

@ -38,7 +38,7 @@ module Biju
# Only keep the bits for the current character and # Only keep the bits for the current character and
# add relevant bits from the previous octet # add relevant bits from the previous octet
# to get the full septet and decode the current character # to get the full septet and decode the current character
current = ((octet & (2 ** (7 - index) - 1)) << index) | next_char current = ((octet & (2**(7 - index) - 1)) << index) | next_char
res = add_char(res, current) res = add_char(res, current)
current_length += 1 current_length += 1

View File

@ -9,7 +9,9 @@ module Biju
def self.encode(string) def self.encode(string)
[ [
string.encode('UCS-2BE').chars.map { |char| "%04x" % char.ord }.join, string.encode('UCS-2BE').chars.map do |char|
'%04x' % char.ord
end.join,
length: string.length * 2, length: string.length * 2,
] ]
end end