Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support OTP-27.0-rc1 (triple-quoted strings and sigil string literals) #89

Merged
merged 4 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude = ["/rebar3_efmt/"]
[dependencies]
anyhow = "1"
clap = { version = "4", features = ["derive"] }
erl_tokenize = "0.5"
erl_tokenize = "0.6"
efmt_core = { path = "efmt_core", version = "0.1.0" }
env_logger = "0.11"
log = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion efmt_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/sile/efmt"
readme = "README.md"

[dependencies]
erl_tokenize = "0.5"
erl_tokenize = "0.6"
efmt_derive = { path = "../efmt_derive", version = "0.1.0" }
log = "0.4"
thiserror = "1"
Expand Down
6 changes: 4 additions & 2 deletions efmt_core/src/items/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::format::Format;
use crate::items::components::{Either, Element, Parenthesized};
use crate::items::symbols::OpenBraceSymbol;
use crate::items::tokens::{
AtomToken, CharToken, FloatToken, IntegerToken, LexicalToken, SymbolToken, VariableToken,
AtomToken, CharToken, FloatToken, IntegerToken, LexicalToken, SigilStringToken, SymbolToken,
VariableToken,
};
use crate::items::Expr;
use crate::parse::{self, Parse};
Expand Down Expand Up @@ -190,13 +191,14 @@ impl Element for FullExpr {
}
}

/// [AtomToken] | [CharToken] | [FloatToken] | [IntegerToken] | [VariableToken] | [StringExpr]
/// [AtomToken] | [CharToken] | [FloatToken] | [IntegerToken] | [VariableToken] | [StringExpr] | [SigilStringToken]
#[derive(Debug, Clone, Span, Parse, Format, Element)]
pub enum LiteralExpr {
Atom(AtomToken),
Char(CharToken),
Float(FloatToken),
Integer(IntegerToken),
SigilString(SigilStringToken),
String(StringExpr),
Variable(VariableToken),
}
Expand Down
22 changes: 22 additions & 0 deletions efmt_core/src/items/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum LexicalToken {
Float(FloatToken),
Integer(IntegerToken),
Keyword(KeywordToken),
SigilString(SigilStringToken),
String(StringToken),
Symbol(SymbolToken),
Variable(VariableToken),
Expand All @@ -26,6 +27,7 @@ impl LexicalToken {
Self::Float(x) => (&mut x.start, &mut x.end),
Self::Integer(x) => (&mut x.start, &mut x.end),
Self::Keyword(x) => (&mut x.start, &mut x.end),
Self::SigilString(x) => (&mut x.start, &mut x.end),
Self::String(x) => (&mut x.start, &mut x.end),
Self::Symbol(x) => (&mut x.start, &mut x.end),
Self::Variable(x) => (&mut x.start, &mut x.end),
Expand Down Expand Up @@ -188,6 +190,26 @@ impl KeywordToken {

impl_traits!(KeywordToken, Keyword);

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SigilStringToken {
start: Position,
end: Position,
}

impl SigilStringToken {
pub fn new(start: Position, end: Position) -> Self {
Self { start, end }
}
}

impl_traits!(SigilStringToken, SigilString);

impl Element for SigilStringToken {
fn is_packable(&self) -> bool {
true
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StringToken {
value: String,
Expand Down
5 changes: 4 additions & 1 deletion efmt_core/src/parse/token_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::items::macros::{Macro, MacroName};
use crate::items::symbols::{OpenParenSymbol, QuestionSymbol};
use crate::items::tokens::{
AtomToken, CharToken, CommentToken, FloatToken, IntegerToken, KeywordToken, LexicalToken,
StringToken, SymbolToken, VariableToken,
SigilStringToken, StringToken, SymbolToken, VariableToken,
};
use crate::parse::{Error, Parse, Result, ResumeParse};
use crate::span::{Position, Span};
Expand Down Expand Up @@ -247,6 +247,9 @@ impl TokenStream {
erl_tokenize::Token::Keyword(x) => {
KeywordToken::new(x.value(), start_position, end_position).into()
}
erl_tokenize::Token::SigilString(_) => {
SigilStringToken::new(start_position, end_position).into()
}
erl_tokenize::Token::String(x) => {
StringToken::new(x.value(), start_position, end_position).into()
}
Expand Down
29 changes: 29 additions & 0 deletions tests/testdata/otp27_strings.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-module(otp27_strings).

-doc """
First line
Second line with "\*not emphasized\* Markdown"
Third line
""".


triple_quoted_strings() ->
X = <<"""
Line 1
Line 2
"""/utf8>>,
X = """"
++ foo() ++
"""",
ok.


sigil_strings() ->
~"abc\d",
~'abc"d',
~b[abc"d],
~R/^from: /i,
~""""
++ foo() ++
"""",
ok.
Loading