Skip to content

Commit

Permalink
Add lexer option to keep shebangs
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Aug 15, 2022
1 parent 0a3c298 commit 84611bb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions compiler/driver/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ pub(crate) fn arguments() -> Result<(Command, GlobalOptions)> {
.arg(
Arg::new(argument::NAME)
.required(true)
.value_parser(WordParser)
.help("The name of the package"),
)
.args(package_creation_arguments),
Expand Down
16 changes: 13 additions & 3 deletions compiler/lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Outcome {

#[derive(Default)]
pub struct Options {
pub keep_shebang: bool,
pub keep_comments: bool,
}

Expand All @@ -35,6 +36,7 @@ fn lex_string(source: String) -> Outcome {
&map[file],
&Options {
keep_comments: true,
keep_shebang: true,
},
)
.lex()
Expand All @@ -45,8 +47,9 @@ pub trait WordExt: Sized {
}

impl WordExt for Word {
// @Beacon @Bug this allows ids like ` foo-bar `
// (and `hey;;;wkwkwkwwkw`)! just write a custom lexer for this!
// @Bug this allows words like ` foo-bar ` (leading & trailing ws)
// @Note and not long before it used to allow `hey;;;wkwkwkwwkw`!
// @Task just write a custom lexer for this!
fn parse(name: String) -> Result<Self, ()> {
let Outcome { tokens, errors } = lex_string(name);

Expand All @@ -58,7 +61,7 @@ impl WordExt for Word {

obtain!(
(tokens.next().ok_or(())?, tokens.next().ok_or(())?),
(BareToken::Word(atom), BareToken::EndOfInput) => atom
(BareToken::Word(word), BareToken::EndOfInput) => word
)
.map(Self::new_unchecked)
.ok_or(())
Expand Down Expand Up @@ -216,12 +219,19 @@ impl<'a> Lexer<'a> {

if let Some('!') = self.peek() {
while let Some(character) = self.peek() {
if self.options.keep_shebang {
self.take();
}
self.advance();

if character == '\n' {
break;
}
}

if self.options.keep_shebang {
self.add(Shebang);
}
} else {
self.lex_punctuation();
}
Expand Down
1 change: 1 addition & 0 deletions compiler/lexer/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn shebang() {
#!/usr/bin/lushui run
it",
vec![
Token::new(span(1, 23), Shebang),
Token::new(span(23, 25), Word("it".into())),
Token::new(span(25, 25), EndOfInput),
],
Expand Down
2 changes: 2 additions & 0 deletions compiler/token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl TokenExt for Token {
#[derive(Clone, PartialEq, Eq, Discriminant, Debug)]
#[discriminant(name: TokenName)]
pub enum BareToken {
Shebang,
Comment,
DocumentationComment,
Word(Atom), // @Task use crate::Word
Expand Down Expand Up @@ -173,6 +174,7 @@ impl fmt::Display for TokenName {
}

f.write_str(match self {
Shebang => "shebang",
Comment => "comment",
DocumentationComment => "documentation comment",
Word => "word",
Expand Down

0 comments on commit 84611bb

Please sign in to comment.