git.delta.rocks / jrsonnet / refs/commits / b5d51b90d7ba

difftreelog

source

crates/jrsonnet-rowan-parser/src/lib.rs1.4 KiBsourcehistory
1#![deny(unused_must_use)]23use event::Sink;4use generated::nodes::{SourceFile, Trivia};5use lex::lex;6use parser::{LocatedSyntaxError, Parser};7pub use rowan;89mod ast;10mod event;11mod generated;12mod language;13mod lex;14mod marker;15mod parser;16mod precedence;17mod string_block;18mod tests;19mod token_set;2021pub use ast::{AstChildren, AstNode, AstToken};22pub use generated::{nodes, syntax_kinds::SyntaxKind};23pub use language::*;24pub use token_set::SyntaxKindSet;2526use self::{27	ast::support,28	generated::nodes::{Expr, ExprBinary, ExprObjExtend},29};3031pub fn parse(input: &str) -> (SourceFile, Vec<LocatedSyntaxError>) {32	let lexemes = lex(input);33	let kinds = lexemes34		.iter()35		.map(|l| l.kind)36		.filter(|k| !Trivia::can_cast(*k))37		.collect();38	let parser = Parser::new(kinds);39	let events = parser.parse();40	let sink = Sink::new(events, &lexemes);4142	let parse = sink.finish();43	(44		SourceFile {45			syntax: parse.syntax(),46		},47		parse.errors,48	)49}50impl ExprBinary {51	pub fn lhs_work(&self) -> Option<Expr> {52		support::child(self.syntax())53	}54	pub fn rhs_work(&self) -> Option<Expr> {55		let mut children = support::children(self.syntax());56		// skip lhs57		children.next()?;58		children.next()59	}60}61impl ExprObjExtend {62	pub fn lhs_work(&self) -> Option<Expr> {63		support::child(self.syntax())64	}65	pub fn rhs_work(&self) -> Option<Expr> {66		let mut children = support::children(self.syntax());67		// skip lhs68		children.next()?;69		children.next()70	}71}