Packages
gpb
3.12.2
5.0.0
4.21.7
4.21.6
4.21.5
4.21.4
4.21.3
4.21.2
4.21.1
4.21.0
4.20.0
4.19.9
4.19.8
4.19.7
4.19.6
4.19.5
4.19.4
4.19.3
4.19.2
4.19.1
4.19.0
4.18.0
4.17.7
4.17.6
4.17.5
4.17.3
4.17.2
4.17.1
4.17.0
4.16.2
4.16.1
4.16.0
4.15.2
4.15.1
4.14.2
4.14.1
4.14.0
4.13.0
4.12.0
4.11.2
4.11.1
4.11.0
4.10.6
4.10.5
4.10.4
4.10.3
4.10.2
4.10.1
4.10.0
4.9.3
4.9.2
4.9.1
4.9.0
4.8.0
4.7.3
4.7.2
4.7.1
4.7.0
4.6.0
4.5.1
4.5.0
4.4.1
4.4.0
4.3.3
4.3.2
4.3.1
4.3.0
4.2.3
4.2.2
4.2.1
4.2.0
4.1.9
4.1.8
4.1.7
4.1.6
4.1.5
4.1.4
4.1.3
4.1.2
4.1.1
4.1.0
4.0.2
4.0.1
4.0.0
3.28.1
3.28.0
3.27.7
3.27.6
3.27.5
3.27.4
3.27.3
3.27.2
3.27.1
3.27.0
3.26.8
3.26.7
3.26.6
3.26.5
3.26.4
3.26.3
3.26.2
3.26.1
3.26.0
3.25.2
3.25.1
3.25.0
3.24.4
3.24.3
3.24.2
3.24.1
3.24.0
3.23.2
3.23.1
3.23.0
3.22.5
3.22.4
3.22.3
3.22.2
3.22.1
3.22.0
3.21.3
3.21.2
3.21.1
3.21.0
3.20.3
3.20.2
3.20.0
3.19.0
3.18.10
3.18.9
3.18.8
3.18.7
3.18.6
3.18.5
3.18.4
3.18.3
3.18.2
3.18.1
3.18.0
3.17.13
3.17.12
3.17.11
3.17.10
3.17.9
3.17.8
3.17.5
3.17.4
3.17.3
3.17.2
3.17.1
3.17.0
3.16.0
3.15.0
3.14.0
3.13.0
3.12.2
3.12.1
3.12.0
3.11.0
A compiler for Google protocol buffer definitions files for Erlang.
Current section
Files
Jump to
Current section
Files
src/gpb_scan.xrl
%% This line tells emacs to use -*- erlang -*- mode for this file
%%% Copyright (C) 2010-2011 Tomas Abrahamsson
%%%
%%% Author: Tomas Abrahamsson <tab@lysator.liu.se>
%%%
%%% This library is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU Lesser General Public
%%% License as published by the Free Software Foundation; either
%%% version 2.1 of the License, or (at your option) any later version.
%%%
%%% This library is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%%% Lesser General Public License for more details.
%%%
%%% You should have received a copy of the GNU Lesser General Public
%%% License along with this library; if not, write to the Free Software
%%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
%%% MA 02110-1301 USA
Definitions.
D = [0-9]
H = [0-9a-fA-F]
WS = [\000-\s]+
Rules.
required : {token, {required,TokenLine}}.
optional : {token, {optional,TokenLine}}.
repeated : {token, {repeated,TokenLine}}.
double : {token, {double,TokenLine}}.
float : {token, {float,TokenLine}}.
int32 : {token, {int32,TokenLine}}.
int64 : {token, {int64,TokenLine}}.
uint32 : {token, {uint32,TokenLine}}.
uint64 : {token, {uint64,TokenLine}}.
sint32 : {token, {sint32,TokenLine}}.
sint64 : {token, {sint64,TokenLine}}.
fixed32 : {token, {fixed32,TokenLine}}.
fixed64 : {token, {fixed64,TokenLine}}.
sfixed32 : {token, {sfixed32,TokenLine}}.
sfixed64 : {token, {sfixed64,TokenLine}}.
bool : {token, {bool,TokenLine}}.
string : {token, {string,TokenLine}}.
bytes : {token, {bytes,TokenLine}}.
package : {token, {package,TokenLine}}.
message : {token, {message,TokenLine}}.
enum : {token, {enum,TokenLine}}.
option : {token, {option,TokenLine}}.
import : {token, {import,TokenLine}}.
default : {token, {default,TokenLine}}.
packed : {token, {packed,TokenLine}}.
deprecated : {token, {deprecated,TokenLine}}.
extensions : {token, {extensions,TokenLine}}.
extend : {token, {extend,TokenLine}}.
to : {token, {to,TokenLine}}.
max : {token, {max,TokenLine}}.
service : {token, {service,TokenLine}}.
rpc : {token, {rpc,TokenLine}}.
returns : {token, {returns,TokenLine}}.
true : {token, {bool_lit,TokenLine,true}}.
false : {token, {bool_lit,TokenLine,false}}.
syntax : {token, {syntax,TokenLine}}.
"(\\x[0-9a-fA-F]|\\[0-7]|\\[abfnrtv?"'\\]|[^"\n])*" :
{token,{str_lit,TokenLine,string_value(TokenChars)}}.
'(\\x[0-9a-fA-F]|\\[0-7]|\\[abfnrtv?"'\\]|[^'\n])*' :
{token,{str_lit,TokenLine,string_value(TokenChars)}}.
(\+|-)?{D}+\.{D}+((E|e)(\+|\-)?{D}+)? :
{token,{float_lit,TokenLine,str_to_float_1(TokenChars)}}.
(\+|-)?\.{D}+((E|e)(\+|\-)?{D}+)? :
{token,{float_lit,TokenLine,str_to_float_2(TokenChars)}}.
(\+|-)?{D}+\.((E|e)(\+|\-)?{D}+)? :
{token,{float_lit,TokenLine,str_to_float_3(TokenChars)}}.
(\+|-)?{D}+(E|e)(\+|\-)?{D}+ :
{token,{float_lit,TokenLine,str_to_float_4(TokenChars)}}.
% Cannot create neither +-inf or nan in erlang...
% (\+|-)?inf : {token,{float_lit,TokenLine,create_inf(TokenChars)}}.
% nan : {token,{float_lit,TokenLine,create_nan()}}.
(\+|-)?0[xX]{H}+ : {token,{hex_lit,TokenLine,hexstr_to_integer(TokenChars)}}.
(\+|-)?0{D}+ : {token,{oct_lit,TokenLine,octstr_to_integer(TokenChars)}}.
(\+|-)?{D}+ : {token,{dec_lit,TokenLine,list_to_integer(TokenChars)}}.
[a-zA-Z_][A-Za-z0-9_]* : {token,{identifier,TokenLine,TokenChars}}.
\. : {token, {'.', TokenLine}}.
; : {token, {';', TokenLine}}.
\{ : {token, {'{', TokenLine}}.
\} : {token, {'}', TokenLine}}.
\[ : {token, {'[', TokenLine}}.
\] : {token, {']', TokenLine}}.
\( : {token, {'(', TokenLine}}.
\) : {token, {')', TokenLine}}.
= : {token, {'=', TokenLine}}.
, : {token, {',', TokenLine}}.
//.*\n : skip_token. %% C++-style comment
//.* : skip_token. %% C++-style comment
(/\*([^*]|(\*+[^*/]))*\*+/)|(//.*) : skip_token. %% C-style comment
{WS} : skip_token.
Erlang code.
-include_lib("eunit/include/eunit.hrl").
string_value(S) -> % S is with start+end quote
%% Strip quotes.
string_val_2(lists:sublist(S, 2, length(S) - 2)).
string_val_2([$\\|Cs]) -> string_escape(Cs);
string_val_2([C|Cs]) -> [C|string_val_2(Cs)];
string_val_2([]) -> [].
-define(is_octal_char(C), $0 =< C, C =< $7).
string_escape("x"++Rest) ->
{HexChars, Rest2} = collect(fun is_hex_char/1, Rest),
[hex_to_integer(HexChars) | string_val_2(Rest2)];
string_escape([Oct|Rest]) when ?is_octal_char(Oct) ->
{OctChars, Rest2} = collect(fun is_oct_char/1, [Oct|Rest]),
[oct_to_integer(OctChars) | string_val_2(Rest2)];
string_escape([C|Rest]) ->
[escape_char(C) | string_val_2(Rest)].
escape_char($a) -> 7; %\a = BEL
escape_char($b) -> $\b; %\b = BS
escape_char($f) -> $\f; %\f = FF
escape_char($n) -> $\n; %\n = LF
escape_char($r) -> $\r; %\r = CR
escape_char($t) -> $\t; %\t = TAB
escape_char($v) -> $\v; %\v = VT
escape_char(C) -> C.
collect(Pred, Str) -> lists:splitwith(Pred, Str).
is_oct_char(C) -> $0 =< C andalso C =< $7.
is_hex_char(C) -> ($0 =< C andalso C =< $9)
orelse ($a =< C andalso C =< $f)
orelse ($A =< C andalso C =< $F).
oct_to_integer(Str) -> erlang:list_to_integer(Str, 8).
hex_to_integer(Str) -> erlang:list_to_integer(Str, 16).
octstr_to_integer(Str) -> oct_to_integer(Str).
hexstr_to_integer("0x"++H) -> hex_to_integer(H);
hexstr_to_integer("0X"++H) -> hex_to_integer(H);
hexstr_to_integer("+0x"++H) -> hex_to_integer(H);
hexstr_to_integer("+0X"++H) -> hex_to_integer(H);
hexstr_to_integer("-0x"++H) -> -hex_to_integer(H);
hexstr_to_integer("-0X"++H) -> -hex_to_integer(H).
str_to_float_1(S) -> list_to_float(S).
str_to_float_2("."++_=S) -> str_to_float_1("0"++S);
str_to_float_2("+."++T) -> str_to_float_1("0."++T);
str_to_float_2("-."++T) -> str_to_float_1("-0."++T).
str_to_float_3(S) -> %% No decimals after `.' Possiby e+-<n> following `.'
{UpToDot, "."++Rest} = collect(fun isnt_dot/1, S),
str_to_float_1(UpToDot++"."++"0"++Rest).
str_to_float_4(S) -> %% Integer preceeding e+-<n>
{UpToDot, Rest} = collect(fun isnt_exp_e/1, S),
str_to_float_1(UpToDot++"."++"0"++Rest).
isnt_dot(C) -> C /= $. .
isnt_exp_e($e) -> false;
isnt_exp_e($E) -> false;
isnt_exp_e(_) -> true.