eini.erl 2.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
%% Licensed to the Apache Software Foundation (ASF) under one
%% or more contributor license agreements.  See the NOTICE file
%% distributed with this work for additional information
%% regarding copyright ownership.  The ASF licenses this file
%% to you under the Apache License, Version 2.0 (the
%% "License"); you may not use this file except in compliance
%% with the License.  You may obtain a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.

Ryosuke Nakai's avatar
Ryosuke Nakai committed
18
-module(eini).
Devin Torres's avatar
Devin Torres committed
19

20
-author('shino@accense.com').
Shunichi Shinohara's avatar
Shunichi Shinohara committed
21

Devin Torres's avatar
Devin Torres committed
22
-export([parse_string/1, parse_file/1]).
23
24
%% for debug use
-export([lex/1, parse_tokens/1]).
Devin Torres's avatar
Devin Torres committed
25

Shunichi Shinohara's avatar
Shunichi Shinohara committed
26
27
28
29
%% TODO(shino): Add spec's

%% Input:
%%
Shunichi Shinohara's avatar
Shunichi Shinohara committed
30
%% [title1]
Shunichi Shinohara's avatar
Shunichi Shinohara committed
31
%% key = value
Shunichi Shinohara's avatar
Shunichi Shinohara committed
32
%% key2 = value2
Shunichi Shinohara's avatar
Shunichi Shinohara committed
33
34
35
%% [title2]
%% key = value
%%
Shunichi Shinohara's avatar
Shunichi Shinohara committed
36
%% Result form:
Shunichi Shinohara's avatar
Shunichi Shinohara committed
37
%%
Shunichi Shinohara's avatar
Shunichi Shinohara committed
38
%% [
39
40
41
%%  {<<"title1">>, [{<<"key">>, <<"value">>},
%%                  {<<"key2">>, <<"value2">>}}],
%%  {<<"title2">>, [{<<"key">>, <<"value">>}]}
Shunichi Shinohara's avatar
Shunichi Shinohara committed
42
%% ].
Shunichi Shinohara's avatar
Shunichi Shinohara committed
43
%%
Devin Torres's avatar
Devin Torres committed
44
parse_string(String) when is_binary(String) ->
45
  parse_string(binary_to_list(String));
46
parse_string(String) when is_list(String) ->
47
48
  case lex(String) of
    {ok, Tokens} ->
Shunichi Shinohara's avatar
Shunichi Shinohara committed
49
      parse_and_validate(Tokens);
Shunichi Shinohara's avatar
Shunichi Shinohara committed
50
51
52
53
    {error, Reason} ->
      {error, Reason}
  end.

Shunichi Shinohara's avatar
Shunichi Shinohara committed
54
parse_and_validate(Tokens) ->
Shunichi Shinohara's avatar
Shunichi Shinohara committed
55
56
  case parse_tokens(Tokens) of
    {ok, Parsed} ->
Shunichi Shinohara's avatar
Shunichi Shinohara committed
57
      validate(Parsed);
58
59
60
    {error, Reason} ->
      {error, Reason}
  end.
Devin Torres's avatar
Devin Torres committed
61
62

parse_file(Filename) ->
63
64
65
66
  case file:read_file(Filename) of
    {ok, Binary} -> parse_string(Binary);
    Error -> Error
  end.
67
68
69
70

lex(String) when is_binary(String) ->
  lex(binary_to_list(String));
lex(String) when is_list(String) ->
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  %% Add \n char at the end if does NOT end by \n
  %% TOD(shino): more simple logic?
  String2 = case String of
              "" ->
                "\n";
              _NotEmpty ->
                case lists:last(String) of
                  $\n ->
                    String;
                  _ ->
                    String ++ "\n"
                end
            end,
  case eini_lexer:string(String2) of
85
86
87
88
    {ok, [{break, _Line}|RestTokens], _EndLine} ->
      {ok, RestTokens};
    {ok, Tokens, _EndLine} ->
      {ok, Tokens};
89
90
91
92
93
94
95
96
97
98
99
    ErrorInfo ->
      {error, ErrorInfo}
  end.
  
parse_tokens(Tokens) ->
  case eini_parser:parse(Tokens) of
    {ok, Res} ->
      {ok, Res};
    {error, {Line, Mod, Reason}} ->
      {error, {Line, Mod:format_error(Reason)}}
  end.
Shunichi Shinohara's avatar
Shunichi Shinohara committed
100

Shunichi Shinohara's avatar
Shunichi Shinohara committed
101
102
103
validate(Parsed) ->
  %% TODO(shino): validate duplicated keys
  {ok, Parsed}.
Shunichi Shinohara's avatar
Shunichi Shinohara committed
104