1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Services to parse models from a text description
16 module model_parser_base
20 # Vector of 3 values, either `x, y, z`, `u, v, z` or `r, g, b`
24 var x
= 0.0 is writable
27 var y
= 0.0 is writable
30 var z
= 0.0 is writable
32 # U value (redirection as `x`)
33 fun u
: Float do return x
35 # Set U value (redirection for `x=`)
36 fun u
=(value
: Float) do x
= value
38 # V value (redirection as `y`)
39 fun v
: Float do return y
41 # Set V value (redirection for `y=`)
42 fun v
=(value
: Float) do y
= value
45 fun r
: Float do return x
47 # Set R value (redirection for `x=`)
48 fun r
=(value
: Float) do x
= value
51 fun g
: Float do return y
53 # Set G value (redirection for `y=`)
54 fun g
=(value
: Float) do y
= value
57 fun b
: Float do return z
59 # Set B value (redirection for `z=`)
60 fun b
=(value
: Float) do z
= value
62 # Return all values into a new `Array[Float]`
63 fun to_a
: Array[Float] do return [x
, y
, z
]
65 redef fun to_s
do return "<{class_name} {x} {y} {z}>"
68 # Vector of 4 values, either `x, y, z, w`, `u, v, z, w` or `r, g, b, a`
73 var w
= 1.0 is writable
75 # A value (redirection to `z`)
76 fun a
: Float do return z
78 # Set A value (redirection for `z=`)
79 fun a
=(value
: Float) do z
= value
81 # Return all values into a new `Array[Float]`
82 redef fun to_a
do return [x
, y
, z
, w
]
84 redef fun to_s
do return "<{class_name} {x} {y} {z} {w}>"
87 redef class StringProcessor
88 # Read a single token after skipping preceding whitespaces
90 # Returns an empty string when at `eof`
91 protected fun read_token
: String
93 while not eof
and src
[pos
].is_whitespace
and src
[pos
] != '\n' do
98 ignore_until_whitespace_or_comment
100 var str
= src
.substring
(start
, ending-start
)
104 # Read 3 or 4 numbers and return them as a `Vec4`
106 # If there is no fourth value, `Vec4::w` is set to 1.0.
107 protected fun read_vec4
: Vec4
114 var wstr
= read_token
115 if wstr
.length
> 0 then
116 vec
.w
= if wstr
.is_numeric
then wstr
.to_f
else 0.0
124 # Read 2 or 3 numbers and return them as a `Vec3`
126 # If there is no third value, `Vec3::z` is set to 0.0.
127 protected fun read_vec3
: Vec3
131 vec
.y
= read_number
# Make optional
133 var wstr
= read_token
134 if wstr
.length
> 0 then
135 vec
.z
= if wstr
.is_numeric
then wstr
.to_f
else 0.0
143 # Advance `pos` until a whitespace or `#` is encountered
144 protected fun ignore_until_whitespace_or_comment
: Int
146 while src
.length
> pos
and not src
[pos
].is_whitespace
and src
[pos
] != '#' do
152 # Read a token and parse it as a `Float`
153 protected fun read_number
: Float
156 return if str
.is_numeric
then str
.to_f
else 0.0
159 # Advance `pos` until the next end of line or a `#`
160 protected fun read_until_eol_or_comment
: String
164 while not eof
and src
[pos
] != '#' and src
[pos
] != '\n' do
168 var str
= src
.substring
(start
, ending-start
)
172 # Advance `pos` to skip the next end of line
173 protected fun skip_eol
176 var c
= src
.chars
[pos
]
178 if c
== '\n' then break