Struct rustracer::scene::InputStream
source · struct InputStream<R: Read> {
reader: R,
location: SourceLocation,
saved_ch: char,
saved_location: SourceLocation,
saved_token: Option<Token>,
spaces: u32,
}
Expand description
A high-level wrapper around a stream, used to parse scene files (yaml formatted).
This class implements a wrapper around a stream,
with the following additional capabilities:
- It tracks the line number and column number;
- It permits to “unread” characters and tokens;
- It tracks the number of spaces that build up a indent block.
Fields§
§reader: R
A stream that implement Read
trait.
location: SourceLocation
A location pointer.
saved_ch: char
Last saved char.
saved_location: SourceLocation
Last saved location.
saved_token: Option<Token>
Last saved token.
spaces: u32
Spaces that build up an indent block.
Implementations§
source§impl<R: Read> InputStream<R>
impl<R: Read> InputStream<R>
sourcepub fn new(reader: R) -> Self
pub fn new(reader: R) -> Self
Create a new InputStream
from a stream that implement Read
trait.
sourcefn update_pos(&mut self, ch: char)
fn update_pos(&mut self, ch: char)
Update location after having read char from the stream.
sourcefn unread_char(&mut self, ch: char)
fn unread_char(&mut self, ch: char)
Push a character back to the stream.
sourcefn skip_comment(&mut self)
fn skip_comment(&mut self)
If a comment character (for yaml is #
) is found,
skip all the next ones until an end-of-line (\n
) or end-of-file (\x00
).
sourcefn skip_whitespaces_and_comments(&mut self)
fn skip_whitespaces_and_comments(&mut self)
Keep reading characters until a non-whitespace/non-comment character is found.
sourcefn count_spaces(&mut self) -> Result<(), SceneErr>
fn count_spaces(&mut self) -> Result<(), SceneErr>
Count spaces that build up a particular indent block.
See parse_colors
for example of usage.
sourcefn parse_string(
&mut self,
token_location: SourceLocation,
delimiter: char
) -> Result<Token, SceneErr>
fn parse_string( &mut self, token_location: SourceLocation, delimiter: char ) -> Result<Token, SceneErr>
Parse string token Token::String
.
sourcefn parse_float(
&mut self,
first_char: char,
token_location: SourceLocation
) -> Result<Token, SceneErr>
fn parse_float( &mut self, first_char: char, token_location: SourceLocation ) -> Result<Token, SceneErr>
Parse literal number (always as f32
) token Token::LiteralNumber
.
sourcefn parse_keyword_or_identifier(
&mut self,
first_char: char,
token_location: SourceLocation
) -> Token
fn parse_keyword_or_identifier( &mut self, first_char: char, token_location: SourceLocation ) -> Token
Parse a keyword token Token::Keyword
or an identifier token Token::Identifier
.
sourcefn read_token(&mut self) -> Result<Token, SceneErr>
fn read_token(&mut self) -> Result<Token, SceneErr>
Read a Token
from the stream.
If successful return a particular variant of Token
enum wrapped inside Result
.
Otherwise return an error of type SceneErr::InvalidCharacter
.
sourcefn unread_token(&mut self, token: Token)
fn unread_token(&mut self, token: Token)
Make as if token
were never read from stream.
sourcefn match_symbol(&mut self, symbol: char) -> Result<(), SceneErr>
fn match_symbol(&mut self, symbol: char) -> Result<(), SceneErr>
Read a token from stream and check that it matches Token::Symbol
.
Otherwise return a SceneErr::NotMatch
error.
sourcefn match_eol_or_inline_comment(&mut self) -> Result<(), SceneErr>
fn match_eol_or_inline_comment(&mut self) -> Result<(), SceneErr>
Match an eof-of-inline or an inline comment, othewise return SceneErr::NotMatch
error.
sourcefn match_whitespaces_and_comments(&mut self) -> Result<(), SceneErr>
fn match_whitespaces_and_comments(&mut self) -> Result<(), SceneErr>
Match whitespaces or an comments, othewise return SceneErr::NotMatch
error.
Unread the token
, and skip nothing only if Token::Keyword
was parsed.
sourcefn match_spaces(&mut self, level: u32, nested: u32) -> Result<(), SceneErr>
fn match_spaces(&mut self, level: u32, nested: u32) -> Result<(), SceneErr>
Match the correct number of spaces for the current indent block.
Otherwise return a SceneErr::NotMatch
error.
sourcefn match_keyword(&mut self, keyword: Keywords) -> Result<(), SceneErr>
fn match_keyword(&mut self, keyword: Keywords) -> Result<(), SceneErr>
Read a token from stream and check that it matches Token::Keyword
and
a particular keywords
Keywords
.
Otherwise return a SceneErr::NotMatch
error.
sourcefn match_keywords(
&mut self,
keywords: &Vec<Keywords>
) -> Result<Keywords, SceneErr>
fn match_keywords( &mut self, keywords: &Vec<Keywords> ) -> Result<Keywords, SceneErr>
Read a token from stream and check that it matches Token::Keyword
and
a particular range of keywords
Keywords
.
Return, wrapped inside a Result
, the keyword.
Otherwise return a SceneErr::NotMatch
error.
sourcefn match_identifier(&mut self) -> Result<(SourceLocation, String), SceneErr>
fn match_identifier(&mut self) -> Result<(SourceLocation, String), SceneErr>
Read a token from stream and check that it matches Token::Identifier
.
Return, wrapped inside a Result
, the identifier location and value.
Otherwise return a SceneErr::NotMatch
error.
sourcefn match_string(&mut self) -> Result<(SourceLocation, String), SceneErr>
fn match_string(&mut self) -> Result<(SourceLocation, String), SceneErr>
Read a token from stream and check that it matches Token::String
.
Return, wrapped inside a Result
, the string value and its location.
Otherwise return a SceneErr::NotMatch
error.
sourcefn match_number(&mut self) -> Result<f32, SceneErr>
fn match_number(&mut self) -> Result<f32, SceneErr>
Read a token from stream and check that it matches Token::LiteralNumber
.
Return, wrapped inside a Result
, the number value.
Otherwise return a SceneErr::NotMatch
error.
sourcefn match_number_cli(&mut self, cli: Cli) -> Result<f32, SceneErr>
fn match_number_cli(&mut self, cli: Cli) -> Result<f32, SceneErr>
Read a token from stream and check that it matches Token::LiteralNumber
or
a Token::Identifier
with a particular string instance, that if match means
that f32
number must be read from cli
.
Return, wrapped inside a Result
, the number value.
Otherwise return a SceneErr::NotMatch
error.
sourcefn parse_color(&mut self, var: &Var) -> Result<Color, SceneErr>
fn parse_color(&mut self, var: &Var) -> Result<Color, SceneErr>
Parse a rgb color Color
from stream combining previous match methods.
A color could be also read from a Token::Identifier
if its string match a particular key of var.colors
map.
Otherwise return a variant of SceneErr
error.
sourcefn parse_vector(&mut self, var: &Var) -> Result<Vector, SceneErr>
fn parse_vector(&mut self, var: &Var) -> Result<Vector, SceneErr>
Parse an xyz vector Vector
from stream combining previous match methods.
Otherwise return a SceneErr::NotMatch
error.
sourcefn parse_color_name(
&mut self,
colors: &mut BTreeMap<String, Color>,
var: &Var
) -> Result<(), SceneErr>
fn parse_color_name( &mut self, colors: &mut BTreeMap<String, Color>, var: &Var ) -> Result<(), SceneErr>
Parse a color from colors block combining parse_color
and put it inside var.colors
map.
Otherwise return a variant of SceneErr
error.
sourcefn parse_colors(
&mut self,
var: &Var
) -> Result<BTreeMap<String, Color>, SceneErr>
fn parse_colors( &mut self, var: &Var ) -> Result<BTreeMap<String, Color>, SceneErr>
Parse colors inside colors block iterating parse_color_name
until the block end.
Otherwise return a variant of SceneErr
error.
sourcefn parse_material(
&mut self,
materials: &mut BTreeMap<String, Material>,
var: &Var
) -> Result<(), SceneErr>
fn parse_material( &mut self, materials: &mut BTreeMap<String, Material>, var: &Var ) -> Result<(), SceneErr>
Parse a material
Material
inside materials block combining
parse_pigment
and parse_brdf
.
And put it inside var.materials
map.
Otherwise return a variant of SceneErr
error.
sourcefn parse_materials(
&mut self,
var: &Var
) -> Result<BTreeMap<String, Material>, SceneErr>
fn parse_materials( &mut self, var: &Var ) -> Result<BTreeMap<String, Material>, SceneErr>
Parse materials inside materials block iterating parse_material
until the block end.
Otherwise return a variant of SceneErr
error.
sourcefn parse_transformation(
&mut self,
transformations: &BTreeMap<String, Transformation>,
var: &Var
) -> Result<Transformation, SceneErr>
fn parse_transformation( &mut self, transformations: &BTreeMap<String, Transformation>, var: &Var ) -> Result<Transformation, SceneErr>
Parse a transformation
Transformation
from stream combining previous match methods.
Otherwise return a SceneErr::NotMatch
error.
sourcefn parse_composed_transformation(
&mut self,
transformations: &mut BTreeMap<String, Transformation>,
var: &Var
) -> Result<(), SceneErr>
fn parse_composed_transformation( &mut self, transformations: &mut BTreeMap<String, Transformation>, var: &Var ) -> Result<(), SceneErr>
Compose multiple transformation
Transformation
into one iterating over
parse_transformation
.
And put it inside var.transformations
map.
Otherwise return a variant of SceneErr
error.
sourcefn parse_transformations(
&mut self,
var: &Var
) -> Result<BTreeMap<String, Transformation>, SceneErr>
fn parse_transformations( &mut self, var: &Var ) -> Result<BTreeMap<String, Transformation>, SceneErr>
Parse transformations inside transformations block iterating
parse_composed_transformation
until the block end.
Otherwise return a variant of SceneErr
error.
sourcefn parse_shape(
&mut self,
var: &Var
) -> Result<Box<dyn RayIntersection>, SceneErr>
fn parse_shape( &mut self, var: &Var ) -> Result<Box<dyn RayIntersection>, SceneErr>
Parse shape inside shapes block using var.materials
and var.transformations
.
Otherwise return a variant of SceneErr
error.
sourcefn parse_shapes(&mut self, var: &Var) -> Result<World, SceneErr>
fn parse_shapes(&mut self, var: &Var) -> Result<World, SceneErr>
Parse shapes inside shapes block iterating
parse_shape
until the block end.
Otherwise return a variant of SceneErr
error.
sourcefn parse_camera(&mut self, var: &Var, cli: Cli) -> Result<Camera, SceneErr>
fn parse_camera(&mut self, var: &Var, cli: Cli) -> Result<Camera, SceneErr>
Parse camera inside camera block using var.materials
and var.transformations
,
and optionally for particular identifiers read standard values from cli
.
Otherwise return a variant of SceneErr
error.
sourcefn parse_scene(&mut self, cli: Cli) -> Result<Scene, SceneErr>
fn parse_scene(&mut self, cli: Cli) -> Result<Scene, SceneErr>
Parse a scene in all its entirety.
Blocks that must exist:
- camera;
- materials;
- shapes.
Optionals:
- colors;
- transformations.
Blocks can be separated by multiple break line.
When a camera and world (list of shapes) are parsed stop scene parsing.