3 # Copyright (c) 2014 Ingo Ruhnke <grumbel@gmail.com>
5 # This software is provided 'as-is', without any express or implied
6 # warranty. In no event will the authors be held liable for any damages
7 # arising from the use of this software.
9 # Permission is granted to anyone to use this software for any purpose,
10 # including commercial applications, and to alter it and redistribute it
11 # freely, subject to the following restrictions:
13 # 1. The origin of this software must not be misrepresented; you must not
14 # claim that you wrote the original software. If you use this software
15 # in a product, an acknowledgment in the product documentation would be
16 # appreciated but is not required.
17 # 2. Altered source versions must be plainly marked as such, and must not be
18 # misrepresented as being the original software.
19 # 3. This notice may not be removed or altered from any source distribution.
42 stack[-2].append(stack.pop())
57 raise Exception("%d:%d: error: unexpected character: '%s'" % (line, column, c))
59 elif state == 'comment':
65 elif state == 'string':
70 stack[-1].append(atom)
75 elif state == 'number':
76 if not c.isdigit() or c != ".":
77 stack[-1].append(int(atom))
83 elif state == 'symbol':
84 if c.isspace() or c == '(' or c == ')':
85 stack[-1].append(atom)
98 raise Exception("error: list not closed")
100 if __name__ == "__main__":
102 result = parse(r'(() ("bar" foo) ()) () bar ')
104 print "2.", parse(""";;comment
105 ("Hello World" 5 1 123) ("Hello" 123 123 "foobar") ;; comment""")
106 print "3.", parse(r'(8(8)8)')
107 print "4.", parse(r'')
108 print "5.", parse(r' ')
109 with codecs.open("white.stf", encoding='utf-8') as fin:
110 print "6.", parse(fin.read())