]> git.lizzy.rs Git - rust.git/commitdiff
flatten modules
authorAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 20 Feb 2019 13:16:14 +0000 (16:16 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 20 Feb 2019 13:16:14 +0000 (16:16 +0300)
17 files changed:
crates/ra_syntax/src/lib.rs
crates/ra_syntax/src/parsing.rs
crates/ra_syntax/src/parsing/builder.rs
crates/ra_syntax/src/parsing/parser_impl.rs
crates/ra_syntax/src/parsing/parser_impl/event.rs
crates/ra_syntax/src/parsing/reparsing.rs
crates/ra_syntax/src/syntax_error.rs [new file with mode: 0644]
crates/ra_syntax/src/syntax_node.rs
crates/ra_syntax/src/syntax_node/syntax_error.rs [deleted file]
crates/ra_syntax/src/syntax_node/syntax_text.rs [deleted file]
crates/ra_syntax/src/syntax_text.rs [new file with mode: 0644]
crates/ra_syntax/src/validation.rs
crates/ra_syntax/src/validation/block.rs
crates/ra_syntax/src/validation/byte.rs
crates/ra_syntax/src/validation/byte_string.rs
crates/ra_syntax/src/validation/char.rs
crates/ra_syntax/src/validation/string.rs

index 755ccd8e00ef0a89d67d2aa982aae9cd91334017..edd5b4a28a384d7eca45970eaeb2751e67db44ef 100644 (file)
 mod validation;
 mod syntax_node;
 mod ptr;
+mod syntax_error;
+mod syntax_text;
 
 pub use rowan::{SmolStr, TextRange, TextUnit};
 pub use crate::{
     ast::AstNode,
     syntax_kinds::SyntaxKind,
-    syntax_node::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreeArc},
+    syntax_error::{SyntaxError, SyntaxErrorKind, Location},
+    syntax_text::SyntaxText,
+    syntax_node::{Direction,  SyntaxNode, WalkEvent, TreeArc},
     ptr::{SyntaxNodePtr, AstPtr},
     parsing::{tokenize, Token},
 };
index 2c92d554eefce8adab0162807dbd671f305f385a..023e1031cd9f0159776d252dc2d0e02286d4c31c 100644 (file)
@@ -8,8 +8,9 @@
 mod grammar;
 
 use crate::{
+    SyntaxError,
     parsing::builder::GreenBuilder,
-    syntax_node::{GreenNode, SyntaxError},
+    syntax_node::GreenNode,
 };
 
 pub use self::lexer::{tokenize, Token};
index 9d7ad06fee69dc4be089c3ad49ea9d48937c63eb..9090c60c2ea6884ea295a3acbcd4146fab27dec2 100644 (file)
@@ -1,8 +1,9 @@
 use crate::{
     parsing::parser_impl::Sink,
-    syntax_node::{GreenNode, RaTypes, SyntaxError},
-    SmolStr, SyntaxKind,
+    syntax_node::{GreenNode, RaTypes},
+    SmolStr, SyntaxKind, SyntaxError,
 };
+
 use rowan::GreenNodeBuilder;
 
 pub(crate) struct GreenBuilder {
index b710e9d5d67e2d6f2c4c0c26343b43ce96fb8db8..8cce1ab01ce1c15f347ee24dd5f2924ee65ebaee 100644 (file)
@@ -5,15 +5,16 @@
 
 use crate::{
     SmolStr,
-    syntax_node::syntax_error::{ParseError, SyntaxError},
+    syntax_error::{ParseError, SyntaxError},
     parsing::{
-    lexer::Token,
-    parser_api::Parser,
-    parser_impl::{
-        event::{Event, EventProcessor},
-        input::{InputPosition, ParserInput},
+        lexer::Token,
+        parser_api::Parser,
+        parser_impl::{
+            event::{Event, EventProcessor},
+            input::{InputPosition, ParserInput},
+        },
     },
-}};
+};
 
 use crate::SyntaxKind::{self, EOF, TOMBSTONE};
 
index fb43e19cc6dd32f443c8282fe81f2688eccebb6c..2ddbdd34d229dc8e36bbf3cbb490c9c61688dbf0 100644 (file)
@@ -13,7 +13,7 @@
     SmolStr,
     SyntaxKind::{self, *},
     TextRange, TextUnit,
-    syntax_node::syntax_error::{
+    syntax_error::{
         ParseError,
         SyntaxError,
         SyntaxErrorKind,
index 0a24dae0ea0d41b6a03d54f2e6b4d030e1efc532..a88f53dae0154c9fde435be1fd67444321938218 100644 (file)
@@ -1,7 +1,8 @@
 use crate::{
     SyntaxKind::*, TextRange, TextUnit,
     algo,
-    syntax_node::{GreenNode, SyntaxError, SyntaxNode},
+    syntax_node::{GreenNode, SyntaxNode},
+    syntax_error::SyntaxError,
     parsing::{
         grammar,
         parser_impl,
diff --git a/crates/ra_syntax/src/syntax_error.rs b/crates/ra_syntax/src/syntax_error.rs
new file mode 100644 (file)
index 0000000..4ff9980
--- /dev/null
@@ -0,0 +1,146 @@
+use std::fmt;
+
+use crate::{TextRange, TextUnit};
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct SyntaxError {
+    kind: SyntaxErrorKind,
+    location: Location,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum Location {
+    Offset(TextUnit),
+    Range(TextRange),
+}
+
+impl Into<Location> for TextUnit {
+    fn into(self) -> Location {
+        Location::Offset(self)
+    }
+}
+
+impl Into<Location> for TextRange {
+    fn into(self) -> Location {
+        Location::Range(self)
+    }
+}
+
+impl SyntaxError {
+    pub fn new<L: Into<Location>>(kind: SyntaxErrorKind, loc: L) -> SyntaxError {
+        SyntaxError { kind, location: loc.into() }
+    }
+
+    pub fn kind(&self) -> SyntaxErrorKind {
+        self.kind.clone()
+    }
+
+    pub fn location(&self) -> Location {
+        self.location.clone()
+    }
+
+    pub fn offset(&self) -> TextUnit {
+        match self.location {
+            Location::Offset(offset) => offset,
+            Location::Range(range) => range.start(),
+        }
+    }
+
+    pub fn add_offset(mut self, plus_offset: TextUnit) -> SyntaxError {
+        self.location = match self.location {
+            Location::Range(range) => Location::Range(range + plus_offset),
+            Location::Offset(offset) => Location::Offset(offset + plus_offset),
+        };
+
+        self
+    }
+}
+
+impl fmt::Display for SyntaxError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        self.kind.fmt(f)
+    }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum SyntaxErrorKind {
+    ParseError(ParseError),
+    UnescapedCodepoint,
+    EmptyChar,
+    UnclosedChar,
+    OverlongChar,
+    EmptyByte,
+    UnclosedByte,
+    OverlongByte,
+    ByteOutOfRange,
+    UnescapedByte,
+    EmptyByteEscape,
+    InvalidByteEscape,
+    TooShortByteCodeEscape,
+    MalformedByteCodeEscape,
+    UnicodeEscapeForbidden,
+    EmptyAsciiEscape,
+    InvalidAsciiEscape,
+    TooShortAsciiCodeEscape,
+    AsciiCodeEscapeOutOfRange,
+    MalformedAsciiCodeEscape,
+    UnclosedUnicodeEscape,
+    MalformedUnicodeEscape,
+    EmptyUnicodeEcape,
+    OverlongUnicodeEscape,
+    UnicodeEscapeOutOfRange,
+    UnclosedString,
+    InvalidSuffix,
+    InvalidBlockAttr,
+    InvalidMatchInnerAttr,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct ParseError(pub String);
+
+impl fmt::Display for SyntaxErrorKind {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        use self::SyntaxErrorKind::*;
+        match self {
+            UnescapedCodepoint => write!(f, "This codepoint should always be escaped"),
+            EmptyAsciiEscape => write!(f, "Empty escape sequence"),
+            InvalidAsciiEscape => write!(f, "Invalid escape sequence"),
+            EmptyChar => write!(f, "Empty char literal"),
+            UnclosedChar => write!(f, "Unclosed char literal"),
+            OverlongChar => write!(f, "Char literal should be one character long"),
+            EmptyByte => write!(f, "Empty byte literal"),
+            UnclosedByte => write!(f, "Unclosed byte literal"),
+            OverlongByte => write!(f, "Byte literal should be one character long"),
+            ByteOutOfRange => write!(f, "Byte should be a valid ASCII character"),
+            UnescapedByte => write!(f, "This byte should always be escaped"),
+            EmptyByteEscape => write!(f, "Empty escape sequence"),
+            InvalidByteEscape => write!(f, "Invalid escape sequence"),
+            TooShortByteCodeEscape => write!(f, "Escape sequence should have two digits"),
+            MalformedByteCodeEscape => write!(f, "Escape sequence should be a hexadecimal number"),
+            UnicodeEscapeForbidden => {
+                write!(f, "Unicode escapes are not allowed in byte literals or byte strings")
+            }
+            TooShortAsciiCodeEscape => write!(f, "Escape sequence should have two digits"),
+            AsciiCodeEscapeOutOfRange => {
+                write!(f, "Escape sequence should be between \\x00 and \\x7F")
+            }
+            MalformedAsciiCodeEscape => write!(f, "Escape sequence should be a hexadecimal number"),
+            UnclosedUnicodeEscape => write!(f, "Missing `}}`"),
+            MalformedUnicodeEscape => write!(f, "Malformed unicode escape sequence"),
+            EmptyUnicodeEcape => write!(f, "Empty unicode escape sequence"),
+            OverlongUnicodeEscape => {
+                write!(f, "Unicode escape sequence should have at most 6 digits")
+            }
+            UnicodeEscapeOutOfRange => write!(f, "Unicode escape code should be at most 0x10FFFF"),
+            UnclosedString => write!(f, "Unclosed string literal"),
+            InvalidSuffix => write!(f, "Invalid literal suffix"),
+            InvalidBlockAttr => {
+                write!(f, "A block in this position cannot accept inner attributes")
+            }
+            InvalidMatchInnerAttr => {
+                write!(f, "Inner attributes are only allowed directly after the opening brace of the match expression")
+            }
+            ParseError(msg) => write!(f, "{}", msg.0),
+        }
+    }
+}
index a0d7c32ecc84c29065a03f041e709d94c972615a..aa627398d2aaebbcc35ae1d76046435486091271 100644 (file)
@@ -1,13 +1,12 @@
-pub mod syntax_error;
-mod syntax_text;
-
 use std::{fmt, borrow::Borrow};
 
-use self::syntax_text::SyntaxText;
-use crate::{SmolStr, SyntaxKind, TextRange};
 use rowan::{Types, TransparentNewType};
 
-pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location};
+use crate::{
+    SmolStr, SyntaxKind, TextRange, SyntaxText,
+    syntax_error::SyntaxError,
+};
+
 pub use rowan::WalkEvent;
 
 #[derive(Debug, Clone, Copy)]
diff --git a/crates/ra_syntax/src/syntax_node/syntax_error.rs b/crates/ra_syntax/src/syntax_node/syntax_error.rs
deleted file mode 100644 (file)
index 4ff9980..0000000
+++ /dev/null
@@ -1,146 +0,0 @@
-use std::fmt;
-
-use crate::{TextRange, TextUnit};
-
-#[derive(Debug, Clone, PartialEq, Eq, Hash)]
-pub struct SyntaxError {
-    kind: SyntaxErrorKind,
-    location: Location,
-}
-
-#[derive(Debug, Clone, PartialEq, Eq, Hash)]
-pub enum Location {
-    Offset(TextUnit),
-    Range(TextRange),
-}
-
-impl Into<Location> for TextUnit {
-    fn into(self) -> Location {
-        Location::Offset(self)
-    }
-}
-
-impl Into<Location> for TextRange {
-    fn into(self) -> Location {
-        Location::Range(self)
-    }
-}
-
-impl SyntaxError {
-    pub fn new<L: Into<Location>>(kind: SyntaxErrorKind, loc: L) -> SyntaxError {
-        SyntaxError { kind, location: loc.into() }
-    }
-
-    pub fn kind(&self) -> SyntaxErrorKind {
-        self.kind.clone()
-    }
-
-    pub fn location(&self) -> Location {
-        self.location.clone()
-    }
-
-    pub fn offset(&self) -> TextUnit {
-        match self.location {
-            Location::Offset(offset) => offset,
-            Location::Range(range) => range.start(),
-        }
-    }
-
-    pub fn add_offset(mut self, plus_offset: TextUnit) -> SyntaxError {
-        self.location = match self.location {
-            Location::Range(range) => Location::Range(range + plus_offset),
-            Location::Offset(offset) => Location::Offset(offset + plus_offset),
-        };
-
-        self
-    }
-}
-
-impl fmt::Display for SyntaxError {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        self.kind.fmt(f)
-    }
-}
-
-#[derive(Debug, Clone, PartialEq, Eq, Hash)]
-pub enum SyntaxErrorKind {
-    ParseError(ParseError),
-    UnescapedCodepoint,
-    EmptyChar,
-    UnclosedChar,
-    OverlongChar,
-    EmptyByte,
-    UnclosedByte,
-    OverlongByte,
-    ByteOutOfRange,
-    UnescapedByte,
-    EmptyByteEscape,
-    InvalidByteEscape,
-    TooShortByteCodeEscape,
-    MalformedByteCodeEscape,
-    UnicodeEscapeForbidden,
-    EmptyAsciiEscape,
-    InvalidAsciiEscape,
-    TooShortAsciiCodeEscape,
-    AsciiCodeEscapeOutOfRange,
-    MalformedAsciiCodeEscape,
-    UnclosedUnicodeEscape,
-    MalformedUnicodeEscape,
-    EmptyUnicodeEcape,
-    OverlongUnicodeEscape,
-    UnicodeEscapeOutOfRange,
-    UnclosedString,
-    InvalidSuffix,
-    InvalidBlockAttr,
-    InvalidMatchInnerAttr,
-}
-
-#[derive(Debug, Clone, PartialEq, Eq, Hash)]
-pub struct ParseError(pub String);
-
-impl fmt::Display for SyntaxErrorKind {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        use self::SyntaxErrorKind::*;
-        match self {
-            UnescapedCodepoint => write!(f, "This codepoint should always be escaped"),
-            EmptyAsciiEscape => write!(f, "Empty escape sequence"),
-            InvalidAsciiEscape => write!(f, "Invalid escape sequence"),
-            EmptyChar => write!(f, "Empty char literal"),
-            UnclosedChar => write!(f, "Unclosed char literal"),
-            OverlongChar => write!(f, "Char literal should be one character long"),
-            EmptyByte => write!(f, "Empty byte literal"),
-            UnclosedByte => write!(f, "Unclosed byte literal"),
-            OverlongByte => write!(f, "Byte literal should be one character long"),
-            ByteOutOfRange => write!(f, "Byte should be a valid ASCII character"),
-            UnescapedByte => write!(f, "This byte should always be escaped"),
-            EmptyByteEscape => write!(f, "Empty escape sequence"),
-            InvalidByteEscape => write!(f, "Invalid escape sequence"),
-            TooShortByteCodeEscape => write!(f, "Escape sequence should have two digits"),
-            MalformedByteCodeEscape => write!(f, "Escape sequence should be a hexadecimal number"),
-            UnicodeEscapeForbidden => {
-                write!(f, "Unicode escapes are not allowed in byte literals or byte strings")
-            }
-            TooShortAsciiCodeEscape => write!(f, "Escape sequence should have two digits"),
-            AsciiCodeEscapeOutOfRange => {
-                write!(f, "Escape sequence should be between \\x00 and \\x7F")
-            }
-            MalformedAsciiCodeEscape => write!(f, "Escape sequence should be a hexadecimal number"),
-            UnclosedUnicodeEscape => write!(f, "Missing `}}`"),
-            MalformedUnicodeEscape => write!(f, "Malformed unicode escape sequence"),
-            EmptyUnicodeEcape => write!(f, "Empty unicode escape sequence"),
-            OverlongUnicodeEscape => {
-                write!(f, "Unicode escape sequence should have at most 6 digits")
-            }
-            UnicodeEscapeOutOfRange => write!(f, "Unicode escape code should be at most 0x10FFFF"),
-            UnclosedString => write!(f, "Unclosed string literal"),
-            InvalidSuffix => write!(f, "Invalid literal suffix"),
-            InvalidBlockAttr => {
-                write!(f, "A block in this position cannot accept inner attributes")
-            }
-            InvalidMatchInnerAttr => {
-                write!(f, "Inner attributes are only allowed directly after the opening brace of the match expression")
-            }
-            ParseError(msg) => write!(f, "{}", msg.0),
-        }
-    }
-}
diff --git a/crates/ra_syntax/src/syntax_node/syntax_text.rs b/crates/ra_syntax/src/syntax_node/syntax_text.rs
deleted file mode 100644 (file)
index 84e5b23..0000000
+++ /dev/null
@@ -1,144 +0,0 @@
-use std::{fmt, ops};
-
-use crate::{SyntaxNode, TextRange, TextUnit};
-
-#[derive(Clone)]
-pub struct SyntaxText<'a> {
-    node: &'a SyntaxNode,
-    range: TextRange,
-}
-
-impl<'a> SyntaxText<'a> {
-    pub(crate) fn new(node: &'a SyntaxNode) -> SyntaxText<'a> {
-        SyntaxText { node, range: node.range() }
-    }
-
-    pub fn chunks(&self) -> impl Iterator<Item = &'a str> {
-        let range = self.range;
-        self.node.descendants().filter_map(move |node| {
-            let text = node.leaf_text()?;
-            let range = range.intersection(&node.range())?;
-            let range = range - node.range().start();
-            Some(&text[range])
-        })
-    }
-
-    pub fn push_to(&self, buf: &mut String) {
-        self.chunks().for_each(|it| buf.push_str(it));
-    }
-
-    pub fn to_string(&self) -> String {
-        self.chunks().collect()
-    }
-
-    pub fn contains(&self, c: char) -> bool {
-        self.chunks().any(|it| it.contains(c))
-    }
-
-    pub fn find(&self, c: char) -> Option<TextUnit> {
-        let mut acc: TextUnit = 0.into();
-        for chunk in self.chunks() {
-            if let Some(pos) = chunk.find(c) {
-                let pos: TextUnit = (pos as u32).into();
-                return Some(acc + pos);
-            }
-            acc += TextUnit::of_str(chunk);
-        }
-        None
-    }
-
-    pub fn len(&self) -> TextUnit {
-        self.range.len()
-    }
-
-    pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> {
-        let range = range.restrict(self.range).unwrap_or_else(|| {
-            panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range)
-        });
-        SyntaxText { node: self.node, range }
-    }
-
-    pub fn char_at(&self, offset: impl Into<TextUnit>) -> Option<char> {
-        let mut start: TextUnit = 0.into();
-        let offset = offset.into();
-        for chunk in self.chunks() {
-            let end = start + TextUnit::of_str(chunk);
-            if start <= offset && offset < end {
-                let off: usize = u32::from(offset - start) as usize;
-                return Some(chunk[off..].chars().next().unwrap());
-            }
-            start = end;
-        }
-        None
-    }
-}
-
-impl<'a> fmt::Debug for SyntaxText<'a> {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        fmt::Debug::fmt(&self.to_string(), f)
-    }
-}
-
-impl<'a> fmt::Display for SyntaxText<'a> {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        fmt::Display::fmt(&self.to_string(), f)
-    }
-}
-
-pub trait SyntaxTextSlice: fmt::Debug {
-    fn restrict(&self, range: TextRange) -> Option<TextRange>;
-}
-
-impl SyntaxTextSlice for TextRange {
-    fn restrict(&self, range: TextRange) -> Option<TextRange> {
-        self.intersection(&range)
-    }
-}
-
-impl SyntaxTextSlice for ops::RangeTo<TextUnit> {
-    fn restrict(&self, range: TextRange) -> Option<TextRange> {
-        if !range.contains_inclusive(self.end) {
-            return None;
-        }
-        Some(TextRange::from_to(range.start(), self.end))
-    }
-}
-
-impl SyntaxTextSlice for ops::RangeFrom<TextUnit> {
-    fn restrict(&self, range: TextRange) -> Option<TextRange> {
-        if !range.contains_inclusive(self.start) {
-            return None;
-        }
-        Some(TextRange::from_to(self.start, range.end()))
-    }
-}
-
-impl SyntaxTextSlice for ops::Range<TextUnit> {
-    fn restrict(&self, range: TextRange) -> Option<TextRange> {
-        TextRange::from_to(self.start, self.end).restrict(range)
-    }
-}
-
-impl From<SyntaxText<'_>> for String {
-    fn from(text: SyntaxText) -> String {
-        text.to_string()
-    }
-}
-
-impl PartialEq<str> for SyntaxText<'_> {
-    fn eq(&self, mut rhs: &str) -> bool {
-        for chunk in self.chunks() {
-            if !rhs.starts_with(chunk) {
-                return false;
-            }
-            rhs = &rhs[chunk.len()..];
-        }
-        rhs.is_empty()
-    }
-}
-
-impl PartialEq<&'_ str> for SyntaxText<'_> {
-    fn eq(&self, rhs: &&str) -> bool {
-        self == *rhs
-    }
-}
diff --git a/crates/ra_syntax/src/syntax_text.rs b/crates/ra_syntax/src/syntax_text.rs
new file mode 100644 (file)
index 0000000..84e5b23
--- /dev/null
@@ -0,0 +1,144 @@
+use std::{fmt, ops};
+
+use crate::{SyntaxNode, TextRange, TextUnit};
+
+#[derive(Clone)]
+pub struct SyntaxText<'a> {
+    node: &'a SyntaxNode,
+    range: TextRange,
+}
+
+impl<'a> SyntaxText<'a> {
+    pub(crate) fn new(node: &'a SyntaxNode) -> SyntaxText<'a> {
+        SyntaxText { node, range: node.range() }
+    }
+
+    pub fn chunks(&self) -> impl Iterator<Item = &'a str> {
+        let range = self.range;
+        self.node.descendants().filter_map(move |node| {
+            let text = node.leaf_text()?;
+            let range = range.intersection(&node.range())?;
+            let range = range - node.range().start();
+            Some(&text[range])
+        })
+    }
+
+    pub fn push_to(&self, buf: &mut String) {
+        self.chunks().for_each(|it| buf.push_str(it));
+    }
+
+    pub fn to_string(&self) -> String {
+        self.chunks().collect()
+    }
+
+    pub fn contains(&self, c: char) -> bool {
+        self.chunks().any(|it| it.contains(c))
+    }
+
+    pub fn find(&self, c: char) -> Option<TextUnit> {
+        let mut acc: TextUnit = 0.into();
+        for chunk in self.chunks() {
+            if let Some(pos) = chunk.find(c) {
+                let pos: TextUnit = (pos as u32).into();
+                return Some(acc + pos);
+            }
+            acc += TextUnit::of_str(chunk);
+        }
+        None
+    }
+
+    pub fn len(&self) -> TextUnit {
+        self.range.len()
+    }
+
+    pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> {
+        let range = range.restrict(self.range).unwrap_or_else(|| {
+            panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range)
+        });
+        SyntaxText { node: self.node, range }
+    }
+
+    pub fn char_at(&self, offset: impl Into<TextUnit>) -> Option<char> {
+        let mut start: TextUnit = 0.into();
+        let offset = offset.into();
+        for chunk in self.chunks() {
+            let end = start + TextUnit::of_str(chunk);
+            if start <= offset && offset < end {
+                let off: usize = u32::from(offset - start) as usize;
+                return Some(chunk[off..].chars().next().unwrap());
+            }
+            start = end;
+        }
+        None
+    }
+}
+
+impl<'a> fmt::Debug for SyntaxText<'a> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        fmt::Debug::fmt(&self.to_string(), f)
+    }
+}
+
+impl<'a> fmt::Display for SyntaxText<'a> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        fmt::Display::fmt(&self.to_string(), f)
+    }
+}
+
+pub trait SyntaxTextSlice: fmt::Debug {
+    fn restrict(&self, range: TextRange) -> Option<TextRange>;
+}
+
+impl SyntaxTextSlice for TextRange {
+    fn restrict(&self, range: TextRange) -> Option<TextRange> {
+        self.intersection(&range)
+    }
+}
+
+impl SyntaxTextSlice for ops::RangeTo<TextUnit> {
+    fn restrict(&self, range: TextRange) -> Option<TextRange> {
+        if !range.contains_inclusive(self.end) {
+            return None;
+        }
+        Some(TextRange::from_to(range.start(), self.end))
+    }
+}
+
+impl SyntaxTextSlice for ops::RangeFrom<TextUnit> {
+    fn restrict(&self, range: TextRange) -> Option<TextRange> {
+        if !range.contains_inclusive(self.start) {
+            return None;
+        }
+        Some(TextRange::from_to(self.start, range.end()))
+    }
+}
+
+impl SyntaxTextSlice for ops::Range<TextUnit> {
+    fn restrict(&self, range: TextRange) -> Option<TextRange> {
+        TextRange::from_to(self.start, self.end).restrict(range)
+    }
+}
+
+impl From<SyntaxText<'_>> for String {
+    fn from(text: SyntaxText) -> String {
+        text.to_string()
+    }
+}
+
+impl PartialEq<str> for SyntaxText<'_> {
+    fn eq(&self, mut rhs: &str) -> bool {
+        for chunk in self.chunks() {
+            if !rhs.starts_with(chunk) {
+                return false;
+            }
+            rhs = &rhs[chunk.len()..];
+        }
+        rhs.is_empty()
+    }
+}
+
+impl PartialEq<&'_ str> for SyntaxText<'_> {
+    fn eq(&self, rhs: &&str) -> bool {
+        self == *rhs
+    }
+}
index 10672d6bf30fed2c944a844e6c6ecbb83d41641f..69958f0d74fc53d14ad62681ffcf2a321c1fe12f 100644 (file)
@@ -5,7 +5,7 @@
 mod block;
 
 use crate::{
-    SourceFile, syntax_node::SyntaxError, AstNode,
+    SourceFile, SyntaxError, AstNode,
     ast,
     algo::visit::{visitor_ctx, VisitorCtx},
 };
index de949d967e9808de5280cf73d0b8ecc68e6ec7d6..f2cf3cbbd88687baf9f44a77f7a4fb4a9d368037 100644 (file)
@@ -1,9 +1,7 @@
 use crate::{SyntaxKind::*,
     ast::{self, AttrsOwner, AstNode},
-    syntax_node::{
-        SyntaxError,
-        SyntaxErrorKind::*,
-    },
+    SyntaxError,
+    SyntaxErrorKind::*,
 };
 
 pub(crate) fn validate_block_node(node: &ast::Block, errors: &mut Vec<SyntaxError>) {
index acdc12552e428c1cccf5a2627ecb5a10e683a8f1..838e7a65fd5b60ef354717aab9114a5832c97a86 100644 (file)
@@ -5,10 +5,8 @@
     string_lexing::{self, StringComponentKind},
     TextRange,
     validation::char,
-    syntax_node::{
-        SyntaxError,
-        SyntaxErrorKind::*,
-    },
+    SyntaxError,
+    SyntaxErrorKind::*,
 };
 
 pub(super) fn validate_byte_node(node: &ast::Byte, errors: &mut Vec<SyntaxError>) {
index 69a98b640f37e146abbe88cbfaba0f34ad863965..64c7054a15f8add6f8e66a9d213bdbe95130fe6c 100644 (file)
@@ -1,10 +1,8 @@
 use crate::{
     ast::{self, AstNode, AstToken},
     string_lexing::{self, StringComponentKind},
-    syntax_node::{
-        SyntaxError,
-        SyntaxErrorKind::*,
-    },
+    SyntaxError,
+    SyntaxErrorKind::*,
 };
 
 use super::byte;
index 26c15e36d23285f1f168ae97f71f61b517ee174b..3169ed5903a875eb85c264c04a19bb3ac82accea 100644 (file)
@@ -8,10 +8,8 @@
     ast::{self, AstNode, AstToken},
     string_lexing::{self, StringComponentKind},
     TextRange,
-    syntax_node::{
-        SyntaxError,
-        SyntaxErrorKind::*,
-    },
+    SyntaxError,
+    SyntaxErrorKind::*,
 };
 
 pub(super) fn validate_char_node(node: &ast::Char, errors: &mut Vec<SyntaxError>) {
index 2f7f9c7c49a4dd0c3cf82db4c25a895465c30aef..d857d088c8028f4e57358a26e95928752579597c 100644 (file)
@@ -1,10 +1,8 @@
 use crate::{
     ast::{self, AstNode, AstToken},
     string_lexing,
-    syntax_node::{
-        SyntaxError,
-        SyntaxErrorKind::*,
-    },
+    SyntaxError,
+    SyntaxErrorKind::*,
 };
 
 use super::char;