Skip to content

Commit 9aebd9e

Browse files
bors[bot]aochagavia
andcommitted
Merge #226
226: Validate byte literals and byte strings r=aochagavia a=aochagavia Co-authored-by: Adolfo Ochagavía <[email protected]>
2 parents a4f7d7a + c96bfe7 commit 9aebd9e

File tree

15 files changed

+1141
-507
lines changed

15 files changed

+1141
-507
lines changed

crates/ra_syntax/src/ast/generated.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,80 @@ impl<R: TreeRoot<RaTypes>> BreakExprNode<R> {
372372

373373
impl<'a> BreakExpr<'a> {}
374374

375+
// Byte
376+
#[derive(Debug, Clone, Copy,)]
377+
pub struct ByteNode<R: TreeRoot<RaTypes> = OwnedRoot> {
378+
pub(crate) syntax: SyntaxNode<R>,
379+
}
380+
pub type Byte<'a> = ByteNode<RefRoot<'a>>;
381+
382+
impl<R1: TreeRoot<RaTypes>, R2: TreeRoot<RaTypes>> PartialEq<ByteNode<R1>> for ByteNode<R2> {
383+
fn eq(&self, other: &ByteNode<R1>) -> bool { self.syntax == other.syntax }
384+
}
385+
impl<R: TreeRoot<RaTypes>> Eq for ByteNode<R> {}
386+
impl<R: TreeRoot<RaTypes>> Hash for ByteNode<R> {
387+
fn hash<H: Hasher>(&self, state: &mut H) { self.syntax.hash(state) }
388+
}
389+
390+
impl<'a> AstNode<'a> for Byte<'a> {
391+
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
392+
match syntax.kind() {
393+
BYTE => Some(Byte { syntax }),
394+
_ => None,
395+
}
396+
}
397+
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
398+
}
399+
400+
impl<R: TreeRoot<RaTypes>> ByteNode<R> {
401+
pub fn borrowed(&self) -> Byte {
402+
ByteNode { syntax: self.syntax.borrowed() }
403+
}
404+
pub fn owned(&self) -> ByteNode {
405+
ByteNode { syntax: self.syntax.owned() }
406+
}
407+
}
408+
409+
410+
impl<'a> Byte<'a> {}
411+
412+
// ByteString
413+
#[derive(Debug, Clone, Copy,)]
414+
pub struct ByteStringNode<R: TreeRoot<RaTypes> = OwnedRoot> {
415+
pub(crate) syntax: SyntaxNode<R>,
416+
}
417+
pub type ByteString<'a> = ByteStringNode<RefRoot<'a>>;
418+
419+
impl<R1: TreeRoot<RaTypes>, R2: TreeRoot<RaTypes>> PartialEq<ByteStringNode<R1>> for ByteStringNode<R2> {
420+
fn eq(&self, other: &ByteStringNode<R1>) -> bool { self.syntax == other.syntax }
421+
}
422+
impl<R: TreeRoot<RaTypes>> Eq for ByteStringNode<R> {}
423+
impl<R: TreeRoot<RaTypes>> Hash for ByteStringNode<R> {
424+
fn hash<H: Hasher>(&self, state: &mut H) { self.syntax.hash(state) }
425+
}
426+
427+
impl<'a> AstNode<'a> for ByteString<'a> {
428+
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
429+
match syntax.kind() {
430+
BYTE_STRING => Some(ByteString { syntax }),
431+
_ => None,
432+
}
433+
}
434+
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
435+
}
436+
437+
impl<R: TreeRoot<RaTypes>> ByteStringNode<R> {
438+
pub fn borrowed(&self) -> ByteString {
439+
ByteStringNode { syntax: self.syntax.borrowed() }
440+
}
441+
pub fn owned(&self) -> ByteStringNode {
442+
ByteStringNode { syntax: self.syntax.owned() }
443+
}
444+
}
445+
446+
447+
impl<'a> ByteString<'a> {}
448+
375449
// CallExpr
376450
#[derive(Debug, Clone, Copy,)]
377451
pub struct CallExprNode<R: TreeRoot<RaTypes> = OwnedRoot> {

crates/ra_syntax/src/ast/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ impl<'a> Char<'a> {
134134
}
135135
}
136136

137+
impl<'a> Byte<'a> {
138+
pub fn text(&self) -> &SmolStr {
139+
&self.syntax().leaf_text().unwrap()
140+
}
141+
}
142+
143+
impl<'a> ByteString<'a> {
144+
pub fn text(&self) -> &SmolStr {
145+
&self.syntax().leaf_text().unwrap()
146+
}
147+
}
148+
137149
impl<'a> String<'a> {
138150
pub fn text(&self) -> &SmolStr {
139151
&self.syntax().leaf_text().unwrap()

crates/ra_syntax/src/grammar.ron

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ Grammar(
412412
"RangeExpr": (),
413413
"BinExpr": (),
414414
"String": (),
415+
"Byte": (),
416+
"ByteString": (),
415417
"Char": (),
416418
"Literal": (),
417419

0 commit comments

Comments
 (0)