Skip to content
This repository has been archived by the owner on Nov 28, 2017. It is now read-only.

Commit

Permalink
fixed parsing of optional ';' after '}', and better error parse error…
Browse files Browse the repository at this point in the history
… message
  • Loading branch information
freke committed Aug 18, 2012
1 parent 2715966 commit b392f4b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
41 changes: 23 additions & 18 deletions src/protobuffs_compile.erl
Expand Up @@ -125,37 +125,42 @@ output_source (Basename, Messages, Enums, Options) ->
%% @hidden
parse_file(FileName) ->
{ok, InFile} = protobuffs_io:open(FileName, [read]),
String = parse_file(InFile,[]),
String = parse_file(InFile,[],1),
ok = file:close(InFile),
{ok,String}.

%% @hidden
parse_file(InFile,Acc) ->
case protobuffs_io:request(InFile) of
{ok,Token,_EndLine} ->
parse_file(InFile,Acc ++ [Token]);
parse_file(InFile,Acc,Line) ->
case protobuffs_io:request(InFile,Line) of
{ok,Token,EndLine} ->
parse_file(InFile,Acc ++ [Token],EndLine);
{error,token} ->
exit(scanning_error);
{eof,_} ->
{eof,EndLine} ->
Acc
end.

generate_output(TokenizedString,Basename,Options) ->
generate_output(Options, Basename, TokenizedString, fun output/4).

generate_output(Options, Basename, TokenizedString, OutputFunction) ->
{ok, FirstParsed} = parse_string(TokenizedString),
ImportPaths = ["./", "src/"
| proplists:get_value(imports_dir, Options, [])],
Parsed = parse_imports(FirstParsed,
ImportPaths),
Collected = protobuffs_compile_lib:collect_full_messages(Parsed),
Messages = protobuffs_compile_lib:resolve_types(Collected#collected.msg,
Collected#collected.enum),
OutputFunction(Basename,
Messages,
Collected#collected.enum,
Options).
case parse_string(TokenizedString) of
{ok, FirstParsed} ->
ImportPaths = ["./", "src/"
| proplists:get_value(imports_dir, Options, [])],
Parsed = parse_imports(FirstParsed,
ImportPaths),
Collected = protobuffs_compile_lib:collect_full_messages(Parsed),
Messages = protobuffs_compile_lib:resolve_types(Collected#collected.msg,
Collected#collected.enum),
OutputFunction(Basename,
Messages,
Collected#collected.enum,
Options);
{error,{Line_number, Module, Message}} ->
error_logger:error_report({Line_number, Module, Message,TokenizedString}),
exit(1)
end.

create_forms(Basename, Messages, Enums) ->
{_,Binary,_} = code:get_object_code(pokemon_pb),
Expand Down
6 changes: 3 additions & 3 deletions src/protobuffs_io.erl
Expand Up @@ -27,7 +27,7 @@

-module(protobuffs_io).

-export([open/2,path_open/3,close/1,format/3,request/1,write_file/2]).
-export([open/2,path_open/3,close/1,format/3,request/2,write_file/2]).

open(File, Options) ->
file:open(File,Options).
Expand All @@ -41,8 +41,8 @@ close(FileRef) ->
format(FileRef, FormatString, WriteFields) ->
io:format(FileRef, FormatString, WriteFields).

request(InFile) ->
io:request(InFile,{get_until,prompt,protobuffs_scanner,token,[1]}).
request(InFile,Line) ->
io:request(InFile,{get_until,prompt,protobuffs_scanner,token,[Line]}).

write_file(File, Bytes) ->
file:write_file(File,Bytes).
1 change: 1 addition & 0 deletions src/protobuffs_parser.yrl
Expand Up @@ -32,6 +32,7 @@ g_element -> g_var integer g_var integer ';' : {'$1', unwrap('$2'), unwrap('$
g_element -> g_var integer g_var g_var ';' : {'$1', unwrap('$2'), '$4'}.
g_element -> g_var g_var '=' g_value ';' : {'$1', '$2', '$4'}.
g_element -> g_message : '$1'.
g_element -> g_message ';' : '$1'.

g_var -> var : unwrap('$1').

Expand Down
3 changes: 2 additions & 1 deletion test/erlang_protobuffs_SUITE.erl
Expand Up @@ -138,9 +138,9 @@ test_proto_files(Config) ->
DataDir = (?config(data_dir, Config)),
NumTests = (?config(num_tests, Config)),
PrivDir = (?config(priv_dir, Config)),
test_server:format("~n===Out ~p===~n", [?config(priv_dir,Config)]),
ProtoFiles = filelib:wildcard(filename:join([DataDir, "proto", "*.proto"])),
ScanProtoFiles = [Filename || Filename <- ProtoFiles , scan_file(DataDir, PrivDir, Filename)],
test_server:format("~n===Out ~p===~n", [?config(priv_dir,Config)]),
Tests = lists:map(fun(Filename) ->
list_to_atom("proper_protobuffs_" ++
filename:basename(Filename,".proto"))
Expand Down Expand Up @@ -227,6 +227,7 @@ run_test(NumTests, Testname, Acc) ->
end.

scan_file(DataDir, PrivDir, Filename) ->
test_server:format("~n===Scan file ~p===~n", [Filename]),
Path = filename:absname(Filename),
Options = [{imports_dir,
[filename:join([DataDir, "proto"]),
Expand Down
2 changes: 1 addition & 1 deletion test/erlang_protobuffs_SUITE_data/proto/enum.proto
Expand Up @@ -5,7 +5,7 @@ message EnumMsg {
value3 = -1;
value4 = -2147483647;
value5 = 2147483648;
}
};

optional Values value = 1;
}
2 changes: 2 additions & 0 deletions test/protobuffs_proper.erl
Expand Up @@ -531,3 +531,5 @@ proper_protobuffs_mixedCase() ->
Decoded = mixedCase_pb:decode_mixedcase(mixedCase_pb:encode_mixedcase(Input)),
compare_messages(Input, Decoded)
end).


0 comments on commit b392f4b

Please sign in to comment.