summaryrefslogtreecommitdiff
path: root/src/parse/parser.rs
diff options
context:
space:
mode:
authorMartin Haug <mhaug@live.de>2021-11-01 13:03:18 +0100
committerMartin Haug <mhaug@live.de>2021-11-05 13:44:50 +0100
commit49fb3cd4e2a5d6997ad4046d3514f154d8c866dd (patch)
tree4fb2a245a4cb84a6ef238ac1bc71786a0996913d /src/parse/parser.rs
parent7d34a548ccd14debe0668e23454e1ced70e485ec (diff)
Code Review: Life is Like a Box of Iterators
Diffstat (limited to 'src/parse/parser.rs')
-rw-r--r--src/parse/parser.rs24
1 files changed, 11 insertions, 13 deletions
diff --git a/src/parse/parser.rs b/src/parse/parser.rs
index 240de43d..374e7c09 100644
--- a/src/parse/parser.rs
+++ b/src/parse/parser.rs
@@ -1,15 +1,14 @@
use std::ops::Range;
use std::rc::Rc;
-use super::{TokenMode, Tokens};
-use crate::source::{SourceFile, SourceId};
+use super::{is_newline, TokenMode, Tokens};
use crate::syntax::{ErrorPosition, Green, GreenData, GreenNode, NodeKind};
use crate::util::EcoString;
/// A convenient token-based parser.
pub struct Parser<'s> {
/// The parsed file.
- source: &'s SourceFile,
+ src: &'s str,
/// An iterator over the source tokens.
tokens: Tokens<'s>,
/// The stack of open groups.
@@ -61,11 +60,11 @@ pub enum Group {
impl<'s> Parser<'s> {
/// Create a new parser for the source string.
- pub fn new(source: &'s SourceFile) -> Self {
- let mut tokens = Tokens::new(source, TokenMode::Markup);
+ pub fn new(src: &'s str) -> Self {
+ let mut tokens = Tokens::new(src, TokenMode::Markup);
let next = tokens.next();
Self {
- source,
+ src,
tokens,
groups: vec![],
next: next.clone(),
@@ -78,11 +77,6 @@ impl<'s> Parser<'s> {
}
}
- /// The id of the parsed source file.
- pub fn id(&self) -> SourceId {
- self.source.id()
- }
-
/// Start a nested node.
///
/// Each start call has to be matched with a call to `end`,
@@ -366,12 +360,16 @@ impl<'s> Parser<'s> {
/// Determine the column index for the given byte index.
pub fn column(&self, index: usize) -> usize {
- self.source.byte_to_column(index).unwrap()
+ self.src[.. index]
+ .chars()
+ .rev()
+ .take_while(|&c| !is_newline(c))
+ .count()
}
/// Slice out part of the source string.
pub fn get(&self, range: Range<usize>) -> &'s str {
- self.source.get(range).unwrap()
+ self.src.get(range).unwrap()
}
/// Continue parsing in a group.