pub struct Buffer { /* private fields */ }Expand description
A growable source file that can be parsed incrementally.
let mut buffer = welly_parser::Buffer::default();
buffer.push_str("hw = \"Hello, world!\\n\";\n");
buffer.push_str("for _ in 10 { print(hw); }");
while let Some(token) = buffer.try_parse() {
println!("{:#?}", token);
}Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn remainder(&self) -> &str
pub fn remainder(&self) -> &str
Returns the suffix of the source code that has not yet been parsed.
Sourcepub fn push_str(&mut self, source: &str)
pub fn push_str(&mut self, source: &str)
Append source to the source code. Requires !self.is_complete().
Sourcepub fn complete(&mut self)
pub fn complete(&mut self)
Inform self that it has all the source code.
This can be important, as in the following example:
let mut buffer = welly_parser::Buffer::default();
buffer.push_str("if c {}"); // Could be followed by `else`.
assert!(buffer.try_parse().is_none());
buffer.complete(); // Exclude `else`.
assert!(buffer.try_parse().is_some());Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Returns true if more source code can be added with self.push_str().
Sourcepub fn try_parse(&mut self) -> Option<(Rc<str>, Token)>
pub fn try_parse(&mut self) -> Option<(Rc<str>, Token)>
Attempt to parse [self.remainder()]. Hopefully the returned Token
is a Stmt. Other possibilities can be found in [welly].
Some((source, token)) indicates that there was enough source code to
parse a Token (which might be an error message). Locations are
relative to the returned source, which is removed from self.
None indicates that there was not enough source code, either because
we need to wait for more, or because there is no more. In this case
self is not modified.