Function parse_string

Source
pub fn parse_string<T: Parse>(source: String) -> Result<T>
Expand description

Scans and parses the given string into the syntax tree node T.

This function ignores all whitespace.

ยงErrors

Forwards any errors from T::parse.

Examples found in repository?
examples/calc.rs (line 96)
95fn main() {
96    let expr: Expr = pretty_unwrap(parse_string(env::args().nth(1).expect("expect expression")));
97    println!("{}", expr.eval());
98}
More examples
Hide additional examples
examples/lox/main.rs (lines 713-752)
712fn main() {
713    let ast: Ast = pretty_unwrap(parse_string(
714        r#"
715        var x = 5;
716        var y = "hello";
717        x = 6;
718        y = 4.0;
719
720        fun add(x, y) {
721            return x + y;
722        }
723
724        print(add(x, y));
725
726        class Cake {
727            init(flavour) {
728                this.flavour = flavour;
729            }
730
731            taste() {
732                var adjective = "delicious";
733                print "The " + this.flavour + " cake is " + adjective + "!";
734            }
735        }
736
737        class ChocolateCake < Cake {
738            init() {
739                this.flavour = "Chocolate";
740            }
741
742            taste() {
743                super.taste();
744                print "Mmm, chocolatey!";
745            }
746        }
747
748        var cake = ChocolateCake();
749        cake.taste();
750    "#
751        .to_string(),
752    ));
753    pretty_unwrap(resolver::resolve(&ast));
754    pretty_unwrap(interpreter::interpret(ast));
755}