Reorganize

This commit is contained in:
Aleksey Kladov 2018-07-29 15:16:07 +03:00
parent ad188d4c3d
commit 415c891d64
20 changed files with 209 additions and 196 deletions

View file

@ -1,7 +1,6 @@
use lexer::ptr::Ptr; use lexer::ptr::Ptr;
use SyntaxKind; use SyntaxKind::{self, *};
use syntax_kinds::*;
pub(crate) fn scan_shebang(ptr: &mut Ptr) -> bool { pub(crate) fn scan_shebang(ptr: &mut Ptr) -> bool {
if ptr.next_is('!') && ptr.nnext_is('/') { if ptr.next_is('!') && ptr.nnext_is('/') {

View file

@ -1,21 +1,32 @@
use {SyntaxKind, Token};
use syntax_kinds::*;
mod ptr; mod ptr;
use self::ptr::Ptr;
mod classes;
use self::classes::*;
mod numbers;
use self::numbers::scan_number;
mod strings;
use self::strings::{is_string_literal_start, scan_byte_char_or_string, scan_char, scan_raw_string,
scan_string};
mod comments; mod comments;
use self::comments::{scan_comment, scan_shebang}; mod strings;
mod numbers;
mod classes;
use {
TextUnit,
SyntaxKind::{self, *},
};
use self::{
ptr::Ptr,
classes::*,
numbers::scan_number,
strings::{
is_string_literal_start, scan_byte_char_or_string, scan_char,
scan_raw_string, scan_string},
comments::{scan_comment, scan_shebang},
};
/// A token of Rust source.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Token {
/// The kind of token.
pub kind: SyntaxKind,
/// The length of the token.
pub len: TextUnit,
}
/// Break a string up into its component tokens /// Break a string up into its component tokens
pub fn tokenize(text: &str) -> Vec<Token> { pub fn tokenize(text: &str) -> Vec<Token> {
@ -29,6 +40,7 @@ pub fn tokenize(text: &str) -> Vec<Token> {
} }
acc acc
} }
/// Get the next token from a string /// Get the next token from a string
pub fn next_token(text: &str) -> Token { pub fn next_token(text: &str) -> Token {
assert!(!text.is_empty()); assert!(!text.is_empty());
@ -109,7 +121,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
DOTDOT DOTDOT
} }
_ => DOT, _ => DOT,
} };
} }
':' => { ':' => {
return match ptr.next() { return match ptr.next() {
@ -118,7 +130,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
COLONCOLON COLONCOLON
} }
_ => COLON, _ => COLON,
} };
} }
'=' => { '=' => {
return match ptr.next() { return match ptr.next() {
@ -131,7 +143,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
FAT_ARROW FAT_ARROW
} }
_ => EQ, _ => EQ,
} };
} }
'!' => { '!' => {
return match ptr.next() { return match ptr.next() {
@ -140,7 +152,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
NEQ NEQ
} }
_ => EXCL, _ => EXCL,
} };
} }
'-' => { '-' => {
return if ptr.next_is('>') { return if ptr.next_is('>') {
@ -148,7 +160,7 @@ fn next_token_inner(c: char, ptr: &mut Ptr) -> SyntaxKind {
THIN_ARROW THIN_ARROW
} else { } else {
MINUS MINUS
} };
} }
// If the character is an ident start not followed by another single // If the character is an ident start not followed by another single
@ -202,7 +214,7 @@ fn scan_ident(c: char, ptr: &mut Ptr) -> SyntaxKind {
return if c == '_' { UNDERSCORE } else { IDENT }; return if c == '_' { UNDERSCORE } else { IDENT };
} }
ptr.bump_while(is_ident_continue); ptr.bump_while(is_ident_continue);
if let Some(kind) = ident_to_keyword(ptr.current_token_text()) { if let Some(kind) = SyntaxKind::from_keyword(ptr.current_token_text()) {
return kind; return kind;
} }
IDENT IDENT

View file

@ -1,8 +1,7 @@
use lexer::ptr::Ptr; use lexer::ptr::Ptr;
use lexer::classes::*; use lexer::classes::*;
use SyntaxKind; use SyntaxKind::{self, *};
use syntax_kinds::*;
pub(crate) fn scan_number(c: char, ptr: &mut Ptr) -> SyntaxKind { pub(crate) fn scan_number(c: char, ptr: &mut Ptr) -> SyntaxKind {
if c == '0' { if c == '0' {

View file

@ -1,5 +1,4 @@
use SyntaxKind; use SyntaxKind::{self, *};
use syntax_kinds::*;
use lexer::ptr::Ptr; use lexer::ptr::Ptr;

View file

@ -19,27 +19,36 @@
extern crate unicode_xid; extern crate unicode_xid;
extern crate text_unit; extern crate text_unit;
mod tree;
mod lexer; mod lexer;
mod parser; mod parser;
mod yellow; mod yellow;
mod syntax_kinds;
pub use {
text_unit::{TextRange, TextUnit},
syntax_kinds::SyntaxKind,
yellow::{SyntaxNode},
lexer::{tokenize, Token},
};
pub(crate) use {
yellow::SyntaxError
};
pub fn parse(text: String) -> SyntaxNode {
let tokens = tokenize(&text);
parser::parse::<yellow::GreenBuilder>(text, &tokens)
}
pub mod syntax_kinds;
pub use text_unit::{TextRange, TextUnit};
pub use tree::{SyntaxKind, Token};
pub(crate) use tree::{Sink, GreenBuilder};
pub use lexer::{next_token, tokenize};
pub use yellow::SyntaxNode;
pub(crate) use yellow::SError;
pub use parser::{parse_green};
/// Utilities for simple uses of the parser. /// Utilities for simple uses of the parser.
pub mod utils { pub mod utils {
use std::fmt::Write; use std::{
fmt::Write,
collections::BTreeSet
};
use {SyntaxNode}; use {SyntaxNode, SyntaxError};
use std::collections::BTreeSet;
use SError;
/// Parse a file and create a string representation of the resulting parse tree. /// Parse a file and create a string representation of the resulting parse tree.
pub fn dump_tree_green(syntax: &SyntaxNode) -> String { pub fn dump_tree_green(syntax: &SyntaxNode) -> String {
@ -48,7 +57,7 @@ pub mod utils {
go(syntax, &mut result, 0, &mut errors); go(syntax, &mut result, 0, &mut errors);
return result; return result;
fn go(node: &SyntaxNode, buff: &mut String, level: usize, errors: &mut BTreeSet<SError>) { fn go(node: &SyntaxNode, buff: &mut String, level: usize, errors: &mut BTreeSet<SyntaxError>) {
buff.push_str(&String::from(" ").repeat(level)); buff.push_str(&String::from(" ").repeat(level));
write!(buff, "{:?}\n", node).unwrap(); write!(buff, "{:?}\n", node).unwrap();
let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().start()) let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().start())

View file

@ -1,8 +1,29 @@
//! This module provides a way to construct a `File`.
//! It is intended to be completely decoupled from the
//! parser, so as to allow to evolve the tree representation
//! and the parser algorithm independently.
//!
//! The `Sink` trait is the bridge between the parser and the
//! tree builder: the parser produces a stream of events like
//! `start node`, `finish node`, and `FileBuilder` converts
//! this stream to a real tree.
use { use {
Sink, SyntaxKind, Token, TextUnit,
syntax_kinds::TOMBSTONE, SyntaxKind::{self, TOMBSTONE},
lexer::Token,
}; };
use super::is_insignificant;
pub(crate) trait Sink {
type Tree;
fn new(text: String) -> Self;
fn leaf(&mut self, kind: SyntaxKind, len: TextUnit);
fn start_internal(&mut self, kind: SyntaxKind);
fn finish_internal(&mut self);
fn error(&mut self, err: String);
fn finish(self) -> Self::Tree;
}
/// `Parser` produces a flat list of `Event`s. /// `Parser` produces a flat list of `Event`s.
/// They are converted to a tree-structure in /// They are converted to a tree-structure in
@ -67,7 +88,7 @@ pub(crate) enum Event {
}, },
} }
pub(super) fn process(builder: &mut Sink, tokens: &[Token], events: Vec<Event>) { pub(super) fn process(builder: &mut impl Sink, tokens: &[Token], events: Vec<Event>) {
let mut idx = 0; let mut idx = 0;
let mut holes = Vec::new(); let mut holes = Vec::new();
@ -111,7 +132,7 @@ pub(super) fn process(builder: &mut Sink, tokens: &[Token], events: Vec<Event>)
&Event::Finish => { &Event::Finish => {
while idx < tokens.len() { while idx < tokens.len() {
let token = tokens[idx]; let token = tokens[idx];
if is_insignificant(token.kind) { if token.kind.is_trivia() {
idx += 1; idx += 1;
builder.leaf(token.kind, token.len); builder.leaf(token.kind, token.len);
} else { } else {
@ -128,7 +149,7 @@ pub(super) fn process(builder: &mut Sink, tokens: &[Token], events: Vec<Event>)
// this should be done in a sensible manner instead // this should be done in a sensible manner instead
loop { loop {
let token = tokens[idx]; let token = tokens[idx];
if !is_insignificant(token.kind) { if !token.kind.is_trivia() {
break; break;
} }
builder.leaf(token.kind, token.len); builder.leaf(token.kind, token.len);

View file

@ -21,11 +21,6 @@
//! After adding a new inline-test, run `cargo collect-tests` to extract //! After adding a new inline-test, run `cargo collect-tests` to extract
//! it as a standalone text-fixture into `tests/data/parser/inline`, and //! it as a standalone text-fixture into `tests/data/parser/inline`, and
//! run `cargo test` once to create the "gold" value. //! run `cargo test` once to create the "gold" value.
use parser::parser::Parser;
use parser::token_set::TokenSet;
use SyntaxKind;
use syntax_kinds::*;
mod items; mod items;
mod attributes; mod attributes;
mod expressions; mod expressions;
@ -34,6 +29,14 @@ mod patterns;
mod paths; mod paths;
mod type_params; mod type_params;
use {
SyntaxKind::{self, *},
parser::{
parser::Parser,
token_set::TokenSet
}
};
pub(crate) fn file(p: &mut Parser) { pub(crate) fn file(p: &mut Parser) {
let file = p.start(); let file = p.start();
p.eat(SHEBANG); p.eat(SHEBANG);

View file

@ -1,6 +1,8 @@
use {SyntaxKind, TextRange, TextUnit, Token}; use {
use syntax_kinds::EOF; SyntaxKind, TextRange, TextUnit,
use super::is_insignificant; SyntaxKind::EOF,
lexer::Token,
};
use std::ops::{Add, AddAssign}; use std::ops::{Add, AddAssign};
@ -16,7 +18,7 @@ impl<'t> ParserInput<'t> {
let mut start_offsets = Vec::new(); let mut start_offsets = Vec::new();
let mut len = 0.into(); let mut len = 0.into();
for &token in raw_tokens.iter() { for &token in raw_tokens.iter() {
if !is_insignificant(token.kind) { if !token.kind.is_trivia() {
tokens.push(token); tokens.push(token);
start_offsets.push(len); start_offsets.push(len);
} }

View file

@ -5,18 +5,16 @@ mod input;
mod event; mod event;
mod grammar; mod grammar;
use std::sync::Arc;
use { use {
Token, lexer::Token,
yellow::SyntaxNode, parser::event::{process}
syntax_kinds::*
}; };
use GreenBuilder;
use parser::event::process; pub(crate) use self::event::Sink;
/// Parse a sequence of tokens into the representative node tree /// Parse a sequence of tokens into the representative node tree
pub fn parse_green(text: String, tokens: &[Token]) -> SyntaxNode { pub(crate) fn parse<S: Sink>(text: String, tokens: &[Token]) -> S::Tree {
let events = { let events = {
let input = input::ParserInput::new(&text, tokens); let input = input::ParserInput::new(&text, tokens);
let parser_impl = parser::imp::ParserImpl::new(&input); let parser_impl = parser::imp::ParserImpl::new(&input);
@ -24,15 +22,7 @@ pub fn parse_green(text: String, tokens: &[Token]) -> SyntaxNode {
grammar::file(&mut parser); grammar::file(&mut parser);
parser.0.into_events() parser.0.into_events()
}; };
let mut builder = GreenBuilder::new(text); let mut sink = S::new(text);
process(&mut builder, tokens, events); process(&mut sink, tokens, events);
let (green, errors) = builder.finish(); sink.finish()
SyntaxNode::new(Arc::new(green), errors)
}
fn is_insignificant(kind: SyntaxKind) -> bool {
match kind {
WHITESPACE | COMMENT => true,
_ => false,
}
} }

View file

@ -1,8 +1,7 @@
use parser::input::{InputPosition, ParserInput}; use parser::input::{InputPosition, ParserInput};
use parser::event::Event; use parser::event::Event;
use SyntaxKind; use SyntaxKind::{self, EOF, TOMBSTONE};
use syntax_kinds::{EOF, TOMBSTONE};
/// Implementation details of `Parser`, extracted /// Implementation details of `Parser`, extracted
/// to a separate struct in order not to pollute /// to a separate struct in order not to pollute

View file

@ -1,5 +1,4 @@
use SyntaxKind; use SyntaxKind::{self, ERROR};
use syntax_kinds::ERROR;
pub(super) mod imp; pub(super) mod imp;
use self::imp::ParserImpl; use self::imp::ParserImpl;

View file

@ -1,7 +1,7 @@
#![allow(bad_style, missing_docs, unreachable_pub)] #![allow(bad_style, missing_docs, unreachable_pub)]
#![cfg_attr(rustfmt, rustfmt_skip)] #![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated from grammar.ron //! Generated from grammar.ron
use tree::SyntaxInfo; use super::SyntaxInfo;
/// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`. /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -288,39 +288,39 @@ impl SyntaxKind {
EOF => &SyntaxInfo { name: "EOF" }, EOF => &SyntaxInfo { name: "EOF" },
} }
} }
} pub(crate) fn from_keyword(ident: &str) -> Option<SyntaxKind> {
match ident {
pub(crate) fn ident_to_keyword(ident: &str) -> Option<SyntaxKind> { "use" => Some(USE_KW),
match ident { "fn" => Some(FN_KW),
"use" => Some(USE_KW), "struct" => Some(STRUCT_KW),
"fn" => Some(FN_KW), "enum" => Some(ENUM_KW),
"struct" => Some(STRUCT_KW), "trait" => Some(TRAIT_KW),
"enum" => Some(ENUM_KW), "impl" => Some(IMPL_KW),
"trait" => Some(TRAIT_KW), "true" => Some(TRUE_KW),
"impl" => Some(IMPL_KW), "false" => Some(FALSE_KW),
"true" => Some(TRUE_KW), "as" => Some(AS_KW),
"false" => Some(FALSE_KW), "extern" => Some(EXTERN_KW),
"as" => Some(AS_KW), "crate" => Some(CRATE_KW),
"extern" => Some(EXTERN_KW), "mod" => Some(MOD_KW),
"crate" => Some(CRATE_KW), "pub" => Some(PUB_KW),
"mod" => Some(MOD_KW), "self" => Some(SELF_KW),
"pub" => Some(PUB_KW), "super" => Some(SUPER_KW),
"self" => Some(SELF_KW), "in" => Some(IN_KW),
"super" => Some(SUPER_KW), "where" => Some(WHERE_KW),
"in" => Some(IN_KW), "for" => Some(FOR_KW),
"where" => Some(WHERE_KW), "loop" => Some(LOOP_KW),
"for" => Some(FOR_KW), "while" => Some(WHILE_KW),
"loop" => Some(LOOP_KW), "if" => Some(IF_KW),
"while" => Some(WHILE_KW), "match" => Some(MATCH_KW),
"if" => Some(IF_KW), "const" => Some(CONST_KW),
"match" => Some(MATCH_KW), "static" => Some(STATIC_KW),
"const" => Some(CONST_KW), "mut" => Some(MUT_KW),
"static" => Some(STATIC_KW), "unsafe" => Some(UNSAFE_KW),
"mut" => Some(MUT_KW), "type" => Some(TYPE_KW),
"unsafe" => Some(UNSAFE_KW), "ref" => Some(REF_KW),
"type" => Some(TYPE_KW), "let" => Some(LET_KW),
"ref" => Some(REF_KW), _ => None,
"let" => Some(LET_KW), }
_ => None,
} }
} }

27
src/syntax_kinds/mod.rs Normal file
View file

@ -0,0 +1,27 @@
mod generated;
use std::fmt;
use ::{SyntaxKind::*};
pub use self::generated::SyntaxKind;
impl fmt::Debug for SyntaxKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let name = self.info().name;
f.write_str(name)
}
}
pub(crate) struct SyntaxInfo {
pub name: &'static str,
}
impl SyntaxKind {
pub(crate) fn is_trivia(self: SyntaxKind) -> bool {
match self {
WHITESPACE | COMMENT | DOC_COMMENT => true,
_ => false,
}
}
}

View file

@ -1,27 +0,0 @@
mod file_builder;
use ::{TextUnit};
use std::{fmt};
pub(crate) use self::file_builder::{Sink, GreenBuilder};
pub use syntax_kinds::SyntaxKind;
impl fmt::Debug for SyntaxKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let name = self.info().name;
f.write_str(name)
}
}
pub(crate) struct SyntaxInfo {
pub name: &'static str,
}
/// A token of Rust source.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Token {
/// The kind of token.
pub kind: SyntaxKind,
/// The length of the token.
pub len: TextUnit,
}

View file

@ -1,36 +1,26 @@
//! This module provides a way to construct a `File`.
//! It is intended to be completely decoupled from the
//! parser, so as to allow to evolve the tree representation
//! and the parser algorithm independently.
//!
//! The `Sink` trait is the bridge between the parser and the
//! tree builder: the parser produces a stream of events like
//! `start node`, `finish node`, and `FileBuilder` converts
//! this stream to a real tree.
use std::sync::Arc; use std::sync::Arc;
use { use {
SyntaxKind, TextRange, TextUnit, SyntaxKind, TextRange, TextUnit,
yellow::GreenNode yellow::{SyntaxNode, GreenNode, SyntaxError},
parser::Sink
}; };
use SError;
pub(crate) trait Sink {
fn leaf(&mut self, kind: SyntaxKind, len: TextUnit);
fn start_internal(&mut self, kind: SyntaxKind);
fn finish_internal(&mut self);
fn error(&mut self, err: String);
}
pub(crate) struct GreenBuilder { pub(crate) struct GreenBuilder {
text: String, text: String,
stack: Vec<GreenNode>, stack: Vec<GreenNode>,
pos: TextUnit, pos: TextUnit,
root: Option<GreenNode>, root: Option<GreenNode>,
errors: Vec<SError>, errors: Vec<SyntaxError>,
} }
impl GreenBuilder { impl GreenBuilder {
pub(crate) fn new(text: String) -> GreenBuilder {
}
impl Sink for GreenBuilder {
type Tree = SyntaxNode;
fn new(text: String) -> Self {
GreenBuilder { GreenBuilder {
text, text,
stack: Vec::new(), stack: Vec::new(),
@ -40,12 +30,6 @@ impl GreenBuilder {
} }
} }
pub(crate) fn finish(self) -> (GreenNode, Vec<SError>) {
(self.root.unwrap(), self.errors)
}
}
impl Sink for GreenBuilder {
fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) { fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) {
let range = TextRange::offset_len(self.pos, len); let range = TextRange::offset_len(self.pos, len);
self.pos += len; self.pos += len;
@ -73,15 +57,12 @@ impl Sink for GreenBuilder {
} }
fn error(&mut self, message: String) { fn error(&mut self, message: String) {
self.errors.push(SError { message, offset: self.pos }) self.errors.push(SyntaxError { message, offset: self.pos })
} }
}
impl SyntaxKind { fn finish(self) -> SyntaxNode {
fn is_trivia(self) -> bool { SyntaxNode::new(Arc::new(self.root.unwrap()), self.errors)
match self {
SyntaxKind::WHITESPACE | SyntaxKind::DOC_COMMENT | SyntaxKind::COMMENT => true,
_ => false
}
} }
} }

View file

@ -1,6 +1,7 @@
mod green; mod green;
mod red; mod red;
mod syntax; mod syntax;
mod builder;
use std::{ use std::{
sync::{Arc, Weak}, sync::{Arc, Weak},
@ -9,7 +10,8 @@ use std::{
pub(crate) use self::{ pub(crate) use self::{
green::{GreenNode, TextLen}, green::{GreenNode, TextLen},
red::RedNode, red::RedNode,
syntax::SError, syntax::SyntaxError,
builder::GreenBuilder,
}; };
pub use self::syntax::SyntaxNode; pub use self::syntax::SyntaxNode;

View file

@ -4,7 +4,8 @@ use std::{
}; };
use { use {
TextRange, TextUnit, SyntaxKind, TextRange, TextUnit,
SyntaxKind::{self, *},
yellow::{Ptr, RedNode, GreenNode, TextLen}, yellow::{Ptr, RedNode, GreenNode, TextLen},
}; };
@ -18,17 +19,17 @@ pub struct SyntaxNode {
#[derive(Clone)] #[derive(Clone)]
pub struct SyntaxRoot { pub struct SyntaxRoot {
red: Arc<RedNode>, red: Arc<RedNode>,
pub(crate) errors: Arc<Vec<SError>>, pub(crate) errors: Arc<Vec<SyntaxError>>,
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub(crate) struct SError { pub(crate) struct SyntaxError {
pub(crate) message: String, pub(crate) message: String,
pub(crate) offset: TextUnit, pub(crate) offset: TextUnit,
} }
impl SyntaxNode { impl SyntaxNode {
pub(crate) fn new(root: Arc<GreenNode>, errors: Vec<SError>) -> SyntaxNode { pub(crate) fn new(root: Arc<GreenNode>, errors: Vec<SyntaxError>) -> SyntaxNode {
let root = Arc::new(RedNode::new_root(root)); let root = Arc::new(RedNode::new_root(root));
let red = Ptr::new(&root); let red = Ptr::new(&root);
let root = SyntaxRoot { red: root, errors: Arc::new(errors) }; let root = SyntaxRoot { red: root, errors: Arc::new(errors) };
@ -123,7 +124,6 @@ impl fmt::Debug for SyntaxNode {
} }
fn has_short_text(kind: SyntaxKind) -> bool { fn has_short_text(kind: SyntaxKind) -> bool {
use syntax_kinds::*;
match kind { match kind {
IDENT | LIFETIME => true, IDENT | LIFETIME => true,
_ => false, _ => false,

View file

@ -1,15 +1,14 @@
extern crate libsyntax2; extern crate libsyntax2;
extern crate testutils; extern crate testutils;
use libsyntax2::{tokenize, parse_green}; use libsyntax2::{parse};
use libsyntax2::utils::{dump_tree_green}; use libsyntax2::utils::{dump_tree_green};
use testutils::dir_tests; use testutils::dir_tests;
#[test] #[test]
fn parser_tests() { fn parser_tests() {
dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {
let tokens = tokenize(text); let file = parse(text.to_string());
let file = parse_green(text.to_string(), &tokens);
dump_tree_green(&file) dump_tree_green(&file)
}) })
} }

View file

@ -36,7 +36,7 @@ impl Grammar {
acc.push_str("#![allow(bad_style, missing_docs, unreachable_pub)]\n"); acc.push_str("#![allow(bad_style, missing_docs, unreachable_pub)]\n");
acc.push_str("#![cfg_attr(rustfmt, rustfmt_skip)]\n"); acc.push_str("#![cfg_attr(rustfmt, rustfmt_skip)]\n");
acc.push_str("//! Generated from grammar.ron\n"); acc.push_str("//! Generated from grammar.ron\n");
acc.push_str("use tree::SyntaxInfo;\n"); acc.push_str("use super::SyntaxInfo;\n");
acc.push_str("\n"); acc.push_str("\n");
let syntax_kinds: Vec<String> = self.tokens let syntax_kinds: Vec<String> = self.tokens
@ -82,19 +82,19 @@ impl Grammar {
acc.push_str(" EOF => &SyntaxInfo { name: \"EOF\" },\n"); acc.push_str(" EOF => &SyntaxInfo { name: \"EOF\" },\n");
acc.push_str(" }\n"); acc.push_str(" }\n");
acc.push_str(" }\n"); acc.push_str(" }\n");
acc.push_str("}\n");
acc.push_str("\n");
// fn ident_to_keyword // fn from_keyword
acc.push_str("pub(crate) fn ident_to_keyword(ident: &str) -> Option<SyntaxKind> {\n"); acc.push_str(" pub(crate) fn from_keyword(ident: &str) -> Option<SyntaxKind> {\n");
acc.push_str(" match ident {\n"); acc.push_str(" match ident {\n");
// NB: no contextual_keywords here! // NB: no contextual_keywords here!
for kw in self.keywords.iter() { for kw in self.keywords.iter() {
write!(acc, " {:?} => Some({}),\n", kw, kw_token(kw)).unwrap(); write!(acc, " {:?} => Some({}),\n", kw, kw_token(kw)).unwrap();
} }
acc.push_str(" _ => None,\n"); acc.push_str(" _ => None,\n");
acc.push_str(" }\n");
acc.push_str(" }\n"); acc.push_str(" }\n");
acc.push_str("}\n"); acc.push_str("}\n");
acc.push_str("\n");
acc acc
} }
} }
@ -104,7 +104,7 @@ fn grammar_file() -> PathBuf {
} }
fn generated_file() -> PathBuf { fn generated_file() -> PathBuf {
base_dir().join("src/syntax_kinds.rs") base_dir().join("src/syntax_kinds/generated.rs")
} }
fn scream(word: &str) -> String { fn scream(word: &str) -> String {

View file

@ -2,13 +2,12 @@ extern crate libsyntax2;
use std::io::Read; use std::io::Read;
use libsyntax2::{parse_green, tokenize}; use libsyntax2::{parse};
use libsyntax2::utils::dump_tree_green; use libsyntax2::utils::dump_tree_green;
fn main() { fn main() {
let text = read_input(); let text = read_input();
let tokens = tokenize(&text); let file = parse(text);
let file = parse_green(text, &tokens);
let tree = dump_tree_green(&file); let tree = dump_tree_green(&file);
println!("{}", tree); println!("{}", tree);
} }