Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added very simple string parsing.

  • Loading branch information...
commit 319bfa76a8e0b037b6506421c346944bd39eddd2 1 parent 195a718
@brixen authored
View
7 lib/poetics/parser/poetics.kpeg
@@ -3,9 +3,10 @@
root = - value? - end
end = !.
-- = (" " | "\t")*
+- = (" " | "\t" | "\n")*
-value = number
+value = string
+ | number
| boolean
@@ -33,6 +34,8 @@ int = < digits > ~number(text)
real = < digits '.' digits ('e' /[-+]/? /[0-9]/+)? > ~number(text)
+string = position '"' < /[^\\"]*/ > '"' ~string_value(text)
+
# keep track of column and line
line = { current_line }
column = { current_column }
View
4 lib/poetics/syntax/ast.rb
@@ -23,5 +23,9 @@ def null_value
def undefined_value
Undefined.new line, column
end
+
+ def string_value(value)
+ String.new line, column, value
+ end
end
end
View
25 lib/poetics/syntax/literal.rb
@@ -1,6 +1,12 @@
module Poetics
module Syntax
- class Number < Node
+ class Value < Node
+ def to_sexp
+ [sexp_name, value, line, column]
+ end
+ end
+
+ class Number < Value
attr_accessor :value
def initialize(line, column, value)
@@ -8,8 +14,8 @@ def initialize(line, column, value)
@value = value.to_f
end
- def to_sexp
- [:number, value, line, column]
+ def sexp_name
+ :number
end
end
@@ -42,5 +48,18 @@ def sexp_name
:undefined
end
end
+
+ class String < Value
+ attr_accessor :value
+
+ def initialize(line, column, text)
+ super
+ @value = text
+ end
+
+ def sexp_name
+ :string
+ end
+ end
end
end
View
13 spec/syntax/literal_spec.rb
@@ -45,3 +45,16 @@
parse { [:undefined, 1, 1] }
end
end
+
+describe "The String node" do
+ relates '"hello, world"' do
+ parse { [:string, "hello, world", 1, 1] }
+ end
+
+ relates <<-ruby do
+ "hello"
+ ruby
+
+ parse { [:string, "hello", 1, 7] }
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.