[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Pingus-CVS] r3479 - trunk/sexpr
From: |
grumbel at BerliOS |
Subject: |
[Pingus-CVS] r3479 - trunk/sexpr |
Date: |
Sat, 3 Nov 2007 12:46:15 +0100 |
Author: grumbel
Date: 2007-11-03 12:46:15 +0100 (Sat, 03 Nov 2007)
New Revision: 3479
Added:
trunk/sexpr/schema_test.rb
Modified:
trunk/sexpr/README
trunk/sexpr/level-syntax.scm
trunk/sexpr/parser.rb
trunk/sexpr/reader.rb
trunk/sexpr/schema.rb
trunk/sexpr/sexpr.rb
Log:
- some more SExpr stuff
Modified: trunk/sexpr/README
===================================================================
--- trunk/sexpr/README 2007-11-03 09:52:50 UTC (rev 3478)
+++ trunk/sexpr/README 2007-11-03 11:46:15 UTC (rev 3479)
@@ -9,11 +9,17 @@
is parsed properly, but something like:
- (foo . (bar . (baz . ()))
+ (foo . ((bar . (baz . ())) . ()))
-isn't handled.
+isn't handled and gives a syntax error, even so they would be
+equivalent in a full SExpr style parser.
-Right: (foo bar)
-Wrong: (foo . bar)
+SExpr isn't meant to be fast, but meant to be as verbose as possible,
+meaning it can give you the line and column of a given SExpr and parse
+comments and whitespace as well if required.
+Beside the basic parser SExpr also contains a xschema-like verifier in
+the form of SExpr::Schema and a simple helper in the form of
+SExpr::Reader that gives XPath like features.
+
# EOF #
Modified: trunk/sexpr/level-syntax.scm
===================================================================
--- trunk/sexpr/level-syntax.scm 2007-11-03 09:52:50 UTC (rev 3478)
+++ trunk/sexpr/level-syntax.scm 2007-11-03 11:46:15 UTC (rev 3479)
@@ -31,20 +31,20 @@
))))
(element (name "objects")
(type (sequence
- (children
- (element (name "groundpiece")
- (type (mapping
- (children
- (element (name "position") (type (vector2i)))
- (element (name "surface") (type (surface)))))))
+ (children
+ (element (name "groundpiece")
+ (type (mapping
+ (children
+ (element (name "position") (type (vector2i)))
+ (element (name "surface") (type (surface)))))))
- (element (name "exit")
- (type (mapping
- (children
- (element (name "position") (type (vector2i)))
- (element (name "surface") (type (surface)))
- (element (name "release-rate") (type (int)))))))
- ))))
+ (element (name "exit")
+ (type (mapping
+ (children
+ (element (name "position") (type (vector2i)))
+ (element (name "surface") (type (surface)))
+ (element (name "release-rate") (type (int)))))))
+ ))))
))))
;; EOF ;;
Modified: trunk/sexpr/parser.rb
===================================================================
--- trunk/sexpr/parser.rb 2007-11-03 09:52:50 UTC (rev 3478)
+++ trunk/sexpr/parser.rb 2007-11-03 11:46:15 UTC (rev 3479)
@@ -19,7 +19,7 @@
module SExpr
class Parser
-
+
def is_digit(c)
return (?0..?9).member?(c)
end
@@ -61,8 +61,10 @@
c == ?-)
end
- def initialize(str)
+ def initialize(str, parse_comments = false, parse_whitespace = false)
@str = str
+ @parse_comments = parse_comments
+ @parse_whitespace = parse_whitespace
end
def parse()
@@ -198,6 +200,7 @@
end
when :parse_string:
+ # FIXME: "foo\\" will be parsed incorrectly
if (c == ?" and @last_c != ?\\) then
submit(:string, true)
end
@@ -260,10 +263,14 @@
@tokens << [:list_end, get_pos()]
when :comment
- # ignore
+ if @parse_comments then
+ @tokens << Comment.new(current_token, get_pos())
+ end
when :whitespace
- # ignore
+ if @parse_whitespace then
+ @tokens << Whitespace.new(current_token, get_pos())
+ end
else
raise "Parser Bug: #{type}"
Modified: trunk/sexpr/reader.rb
===================================================================
--- trunk/sexpr/reader.rb 2007-11-03 09:52:50 UTC (rev 3478)
+++ trunk/sexpr/reader.rb 2007-11-03 11:46:15 UTC (rev 3479)
@@ -36,7 +36,7 @@
elsif sexpr.length() == 0 then
raise "#{sexpr.pos}: Error: Reader expected List with one or more
elements"
else
- @name = sexpr[0]
+ @name = sexpr[0].value
@sexpr = sexpr[1..-1]
end
end
Modified: trunk/sexpr/schema.rb
===================================================================
--- trunk/sexpr/schema.rb 2007-11-03 09:52:50 UTC (rev 3478)
+++ trunk/sexpr/schema.rb 2007-11-03 11:46:15 UTC (rev 3479)
@@ -16,6 +16,7 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
require "sexpr.rb"
+require "reader.rb"
require "parser.rb"
module SExpr
@@ -24,10 +25,26 @@
if schema.is_a?(SExpr) then
@schema = schema
else
- @schema SExpr.parse(schema)
+ @schema = SExpr.parse(schema)
+ if @schema.length() == 1 then
+ @schema = @schema[0]
+ else
+ raise "Expected exactly one SExpr, got address@hidden"
+ end
end
+
+ parse_scheme(@schema)
end
+ def parse_scheme(schema)
+ reader = Reader.new(schema)
+ if reader.name != "element" then
+ raise "Expected 'element' tag, got '#{reader.name}'"
+ else
+ @root = Element.new(reader)
+ end
+ end
+
def validate(sexpr)
end
@@ -37,6 +54,11 @@
attr_reader :name # name of the expected element
attr_reader :use # required, optional, forbidden
attr_reader :type # ListType, IntegerType, ...
+
+ def initialize(reader)
+ @use = reader.read_string("use")
+ @name = reader.read_string("name")
+ end
def validate(sexpr)
if not sexpr.is_a?(List) then
Added: trunk/sexpr/schema_test.rb
===================================================================
--- trunk/sexpr/schema_test.rb 2007-11-03 09:52:50 UTC (rev 3478)
+++ trunk/sexpr/schema_test.rb 2007-11-03 11:46:15 UTC (rev 3479)
@@ -0,0 +1,17 @@
+#!/usr/bin/ruby -w
+
+require "sexpr.rb"
+require "parser.rb"
+require "schema.rb"
+
+if ARGV.length != 2 then
+ puts "Usage: schema_test.rb SCHEMAFILE SCMFILE"
+else
+ schema = SExpr::Schema.new(File.new(ARGV[0]).read())
+ sexpr = SExpr::SExpr.parse(File.new(ARGV[1]).read())
+
+ schema.validate(sexpr)
+end
+
+# EOF #
+
Property changes on: trunk/sexpr/schema_test.rb
___________________________________________________________________
Name: svn:executable
+ *
Modified: trunk/sexpr/sexpr.rb
===================================================================
--- trunk/sexpr/sexpr.rb 2007-11-03 09:52:50 UTC (rev 3478)
+++ trunk/sexpr/sexpr.rb 2007-11-03 11:46:15 UTC (rev 3479)
@@ -162,6 +162,20 @@
return @value.map{|el| el.to_ruby()}
end
end
+
+ def Whitespace
+ def initialize(value, pos = nil)
+ super(pos)
+ @value = value
+ end
+ end
+
+ def Commont
+ def initialize(value, pos = nil)
+ super(pos)
+ @value = value
+ end
+ end
end
# EOF #
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Pingus-CVS] r3479 - trunk/sexpr,
grumbel at BerliOS <=