~ubuntu-branches/debian/sid/f-spot/sid

« back to all changes in this revision

Viewing changes to lib/Hyena/src/Hyena/Hyena.SExpEngine/Parser.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2010-05-24 10:35:57 UTC
  • mfrom: (2.4.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20100524103557-1j0i8f66caybci2n
* New upstream release 0.7.0
 + First release of the unstable 0.7 development series. Massive changes.
 + Reparenting and detaching support (Anton Keks) (Closes: #559745)
 + A new Mallard-based documentation (Harold Schreckengost)
 + No longer embeds flickrnet, uses distribution copy (Iain Lane)
 + Adoption of a large amount of Hyena functionality (Paul Lange, Peter
   Goetz)
 + No longer embeds gnome-keyring-sharp
 + Completely rewritten import, much faster and less memory hungry (Ruben
   Vermeersch) (Closes: #559080, #492658, #341790, #357811, #426017) (LP:
   #412091)
 + No longer use gphoto2-sharp, now uses gvfs which is less crash-pron
   (Ruben Vermeersch)
 + Fix Facebook support (Ruben Vermeersch)
 + Modernized unit tests
 + Revamped build (Mike Gemünde)
 + Much improved duplicate detection (much faster too) (Ruben Vermeersch)
 + Mouse selection in Iconview (Vincent Pomey)
 + Image panning support using middle mouse button (Wojciech Dzierżanowski)
 + Timeline slider now restricted to the size of the window (Iain Churcher)
 + Over 100 bugs closed (http://bit.ly/cyVjnD)
   - No more warnings about schema defaults (Closes: #584215) (LP: #586132)
* debian/control: Clean up build deps to match configure checks
* debian/rules: Don't run dh_makeshilbs as we don't ship any shared
  libraries. There are some private ones though, which get picked up and
  result in a useless postinst/postrm call to ldconfig. Thanks, lintian.
* debian/patches/debian_fix-distclean.patch,
  debian/patches/debian_fix_f-spot.exe.config.patch,
  debian/patches/debian_link-system-flickrnet.patch,
  debian/patches/debian_link-system-gnome-keyring.patch,
  debian/patches/debian_disable-unit-tests,
  debian/patches/git_transition_duration.patch,
  debian/patches/ubuntu_fix_folder_export_hang.patch:
  Clean up obsolete patches which are no longer necessary 
* debian/patches/*: Temporarily disable patches which originated from Ubuntu
  and no longer apply cleanly. We will get these back in a future upstream
  development release.
* debian/patches/*: Refresh to apply cleanly 
* debian/rules: Add new include dir to autoreconf call to pick up f-spot
  macros 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Parser.cs
 
3
//
 
4
// Author:
 
5
//   Aaron Bockover <abockover@novell.com>
 
6
//
 
7
// Copyright (C) 2007-2008 Novell, Inc.
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
//
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
//
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.IO;
 
31
using System.Text;
 
32
using System.Text.RegularExpressions;
 
33
using System.Globalization;
 
34
 
 
35
namespace Hyena.SExpEngine
 
36
{
 
37
    public class ParserException : ApplicationException
 
38
    {
 
39
        public ParserException(string token, int line, int col, Exception inner) : base(String.Format(
 
40
            "Parser exception at token `{0}' [{1},{2}]: {3}",
 
41
            token, line, col, inner == null ? "Unknown error" : inner.Message), inner)
 
42
        {
 
43
        }
 
44
    }
 
45
 
 
46
    public class Parser
 
47
    {
 
48
        private static Regex number_regex = new Regex(@"^[-+]?(0x[\dA-Fa-f]+)?[\d]*\.?[\d]*([eE][-+]?[\d]+)?$");
 
49
        private static System.Globalization.CultureInfo culture_info = new System.Globalization.CultureInfo("en-US");
 
50
 
 
51
        private StreamReader reader;
 
52
        private bool debug;
 
53
 
 
54
        private StringBuilder current_token;
 
55
        private TreeNode root_node ;
 
56
        private TreeNode current_parent;
 
57
        private int scope;
 
58
        private int line;
 
59
        private int column;
 
60
 
 
61
        public Parser()
 
62
        {
 
63
        }
 
64
 
 
65
        public TreeNode Parse(string input)
 
66
        {
 
67
            return Parse(new MemoryStream(Encoding.UTF8.GetBytes(input)));
 
68
        }
 
69
 
 
70
        public TreeNode Parse(Stream stream)
 
71
        {
 
72
            return Parse(new StreamReader(stream));
 
73
        }
 
74
 
 
75
        public TreeNode Parse(StreamReader reader)
 
76
        {
 
77
            this.reader = reader;
 
78
 
 
79
            current_token = new StringBuilder();
 
80
            root_node = new TreeNode();
 
81
            current_parent = root_node;
 
82
            scope = 0;
 
83
            line = 1;
 
84
            column = 0;
 
85
 
 
86
            try {
 
87
                Tokenize();
 
88
 
 
89
                if(scope != 0) {
 
90
                    throw new ApplicationException("Scope does pop back to zero");
 
91
                }
 
92
            } catch(Exception e) {
 
93
                throw new ParserException(current_token.ToString(), line, column, e);
 
94
            }
 
95
 
 
96
            return root_node;
 
97
        }
 
98
 
 
99
        private void Tokenize()
 
100
        {
 
101
            bool in_string = false;
 
102
            bool in_comment = false;
 
103
 
 
104
            while(true) {
 
105
                int ich = reader.Read();
 
106
                char ch = (char)ich;
 
107
 
 
108
                if(ich < 0) {
 
109
                    break;
 
110
                }
 
111
 
 
112
                if(ch == '\n') {
 
113
                    line++;
 
114
                    column = 0;
 
115
                } else {
 
116
                    column++;
 
117
                }
 
118
 
 
119
                if(in_comment) {
 
120
                    if(ch == '\r' || ch == '\n') {
 
121
                        in_comment = false;
 
122
                    }
 
123
 
 
124
                    continue;
 
125
                }
 
126
 
 
127
                switch(ch) {
 
128
                    case '(':
 
129
                    case ')':
 
130
                        if(!in_string) {
 
131
                            TokenPush(false);
 
132
 
 
133
                            if(ch == '(') {
 
134
                                ScopePush();
 
135
                            } else {
 
136
                                ScopePop();
 
137
                            }
 
138
                        } else {
 
139
                            current_token.Append(ch);
 
140
                        }
 
141
                        break;
 
142
                    case '"':
 
143
                        if(in_string) {
 
144
                            in_string = false;
 
145
                            TokenPush(true);
 
146
                        } else {
 
147
                            in_string = true;
 
148
                        }
 
149
                        break;
 
150
                    case '\\':
 
151
                        if((char)reader.Peek() == '"') {
 
152
                            current_token.Append((char)reader.Read());
 
153
                        }
 
154
                        break;
 
155
                    case ';':
 
156
                        if(in_string) {
 
157
                            current_token.Append(ch);
 
158
                        } else {
 
159
                            TokenPush(false);
 
160
                            in_comment = true;
 
161
                        }
 
162
                        break;
 
163
                    default:
 
164
                        if(Char.IsWhiteSpace(ch)) {
 
165
                            if(in_string) {
 
166
                                current_token.Append(ch);
 
167
                            } else {
 
168
                                TokenPush(false);
 
169
                            }
 
170
                        } else {
 
171
                            current_token.Append(ch);
 
172
                        }
 
173
                        break;
 
174
                }
 
175
            }
 
176
 
 
177
            TokenPush(false);
 
178
 
 
179
            reader.Close();
 
180
        }
 
181
 
 
182
        private void ScopePush()
 
183
        {
 
184
            current_parent = new TreeNode(current_parent);
 
185
            scope++;
 
186
        }
 
187
 
 
188
        private void ScopePop()
 
189
        {
 
190
            current_parent = current_parent.Parent;
 
191
            scope--;
 
192
        }
 
193
 
 
194
        private void TokenPush(bool as_string)
 
195
        {
 
196
            if(current_token.Length == 0 && !as_string) {
 
197
                return;
 
198
            }
 
199
 
 
200
            TreeNode node = null;
 
201
            string token = current_token.ToString();
 
202
 
 
203
            if(Debug) {
 
204
                Console.Write("{3}[{0}] TOKEN({4},{5}): [{2}{1}{2}]", scope, token,
 
205
                    as_string ? "\"" : String.Empty, String.Empty.PadLeft(scope - 1, ' '),
 
206
                    line, column - current_token.Length);
 
207
            }
 
208
 
 
209
            if(as_string) {
 
210
                node = new StringLiteral(token);
 
211
            } else if(token == "#t") {
 
212
                node = new BooleanLiteral(true);
 
213
            } else if(token == "#f") {
 
214
                node = new BooleanLiteral(false);
 
215
            } else if(token.Length > 0 && token != "." && token != "-" &&
 
216
                token != "+" && number_regex.IsMatch(token)) {
 
217
                try {
 
218
                    if(token.StartsWith("0x") || token.StartsWith("-0x")) {
 
219
                        int offset = token[0] == '-' ? 3 : 2;
 
220
                        int value = Int32.Parse(token.Substring(offset),
 
221
                            NumberStyles.HexNumber, culture_info.NumberFormat);
 
222
                        node = new IntLiteral(value * (offset == 3 ? -1 : 1));
 
223
                    } else if(token.Contains(".")) {
 
224
                        node = new DoubleLiteral(Double.Parse(token,
 
225
                            NumberStyles.Float, culture_info.NumberFormat));
 
226
                    } else {
 
227
                        node = new IntLiteral(Int32.Parse(token,
 
228
                            NumberStyles.Integer, culture_info.NumberFormat));
 
229
                    }
 
230
                } catch {
 
231
                    throw new FormatException("Invalid number format: " + token);
 
232
                }
 
233
            } else {
 
234
                node = new FunctionNode(token);
 
235
            }
 
236
 
 
237
            if(Debug) {
 
238
                Console.WriteLine(" => [{0}]", node);
 
239
            }
 
240
 
 
241
            node.Line = line;
 
242
            node.Column = column;
 
243
            current_parent.AddChild(node);
 
244
 
 
245
            current_token.Remove(0, current_token.Length);
 
246
        }
 
247
 
 
248
        public bool Debug {
 
249
            get { return debug; }
 
250
            set { debug = value; }
 
251
        }
 
252
    }
 
253
}