Skip to main content

nifty_syntax/
lib.rs

1//! Front end of the Nifty language: source text to a syntax tree, with diagnostics.
2
3pub mod ast;
4mod diagnostic;
5mod lexer;
6mod parser;
7pub mod project;
8mod span;
9mod token;
10
11pub use diagnostic::{Diagnostic, Note};
12pub use lexer::ESCAPES;
13pub use span::{FileId, Span};
14pub use token::{KEYWORDS, SYMBOLS, Token, TokenKind};
15
16/// The result of parsing one source file. The tree is complete even when there are
17/// diagnostics: parts that failed to parse are skipped or marked `ExprKind::Error`.
18#[derive(Debug)]
19pub struct Parsed {
20    pub file: ast::File,
21    pub diagnostics: Vec<Diagnostic>,
22}
23
24/// The tokens of `source`, ending with `Eof`. Lexing errors are left to [`parse`].
25#[must_use]
26pub fn tokens(source: &str, file: FileId) -> Vec<Token> {
27    lexer::lex(source, file).0
28}
29
30/// `text` as a Nifty string literal, quotes included, with every special character escaped.
31#[must_use]
32pub fn string_literal(text: &str) -> String {
33    format!("\"{}\"", lexer::escape(text))
34}
35
36/// Parses the text of `file`; every span in the result points into it.
37#[must_use]
38pub fn parse(source: &str, file: FileId) -> Parsed {
39    let (file, diagnostics) = parser::parse(source, file);
40    Parsed { file, diagnostics }
41}