mirror of
https://github.com/erg-lang/erg.git
synced 2025-07-07 21:25:31 +00:00
Add raw identifier
This commit is contained in:
parent
48eb3c5920
commit
03a36f48a3
14 changed files with 137 additions and 26 deletions
|
@ -1648,11 +1648,6 @@ impl CodeGenerator {
|
|||
self.emit_expr(*tasc.expr);
|
||||
}
|
||||
Expr::Import(acc) => self.emit_import(acc),
|
||||
other => {
|
||||
CompileError::feature_error(self.cfg.input.clone(), other.loc(), "??", "".into())
|
||||
.write_to_stderr();
|
||||
self.crash("cannot compile this expression at this time");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1641,6 +1641,11 @@ impl Context {
|
|||
NoneType,
|
||||
);
|
||||
let t_bin = nd_func(vec![kw("n", Int)], None, Str);
|
||||
let t_bytes = nd_func(
|
||||
vec![kw("str", Str), kw("encoding", Str)],
|
||||
None,
|
||||
mono("Bytes"),
|
||||
);
|
||||
let t_chr = nd_func(
|
||||
vec![kw("i", Type::from(value(0usize)..=value(1_114_111usize)))],
|
||||
None,
|
||||
|
@ -1742,6 +1747,7 @@ impl Context {
|
|||
self.register_builtin_py_impl("ascii", t_ascii, Immutable, Private, Some("ascii"));
|
||||
self.register_builtin_impl("assert", t_assert, Const, Private); // assert casting に悪影響が出る可能性があるため、Constとしておく
|
||||
self.register_builtin_py_impl("bin", t_bin, Immutable, Private, Some("bin"));
|
||||
self.register_builtin_py_impl("bytes", t_bytes, Immutable, Private, Some("bytes"));
|
||||
self.register_builtin_py_impl("chr", t_chr, Immutable, Private, Some("chr"));
|
||||
self.register_builtin_py_impl("classof", t_classof, Immutable, Private, Some("type"));
|
||||
self.register_builtin_py_impl("compile", t_compile, Immutable, Private, Some("compile"));
|
||||
|
|
|
@ -183,7 +183,7 @@ impl Context {
|
|||
self.impl_of(),
|
||||
py_name,
|
||||
);
|
||||
log!(info "Registered {}::{}: {} {:?}", self.name, ident.name, vi.t, vi.impl_of);
|
||||
log!(info "Registered {}::{}: {}", self.name, ident.name, vi);
|
||||
self.locals.insert(ident.name.clone(), vi);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -841,10 +841,6 @@ impl Context {
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
hir::Expr::Decl(decl) => {
|
||||
decl.t = self.deref_tyvar(mem::take(&mut decl.t), Covariant, decl.loc())?;
|
||||
Ok(())
|
||||
}
|
||||
hir::Expr::Def(def) => {
|
||||
// It is not possible to further dereference the quantified type.
|
||||
// TODO: However, it is possible that there are external type variables within the quantified type.
|
||||
|
|
|
@ -3,6 +3,8 @@ use std::fmt;
|
|||
|
||||
use erg_common::dict::Dict as HashMap;
|
||||
use erg_common::error::Location;
|
||||
#[allow(unused_imports)]
|
||||
use erg_common::log;
|
||||
use erg_common::traits::{Locational, NestedDisplay, Stream};
|
||||
use erg_common::vis::{Field, Visibility};
|
||||
use erg_common::Str;
|
||||
|
@ -547,10 +549,22 @@ impl Accessor {
|
|||
|
||||
pub fn local_name(&self) -> Option<&str> {
|
||||
match self {
|
||||
Self::Ident(ident) => ident.qual_name.as_ref().map(|s| {
|
||||
let mut seps = s.split_with(&[".", "::"]);
|
||||
seps.remove(seps.len() - 1)
|
||||
}),
|
||||
Self::Ident(ident) => ident
|
||||
.qual_name
|
||||
.as_ref()
|
||||
.map(|s| {
|
||||
let mut seps = s.split_with(&[".", "::"]);
|
||||
seps.remove(seps.len() - 1)
|
||||
})
|
||||
.or_else(|| {
|
||||
let mut raw_parts = ident.name.inspect().split_with(&["'"]);
|
||||
// "'aaa'".split_with(&["'"]) == ["", "aaa", ""]
|
||||
if raw_parts.len() == 3 || raw_parts.len() == 4 {
|
||||
Some(raw_parts.remove(1))
|
||||
} else {
|
||||
Some(ident.name.inspect())
|
||||
}
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
@ -1810,7 +1824,6 @@ pub enum Expr {
|
|||
UnaryOp(UnaryOp),
|
||||
Call(Call),
|
||||
Lambda(Lambda),
|
||||
Decl(Decl),
|
||||
Def(Def),
|
||||
ClassDef(ClassDef),
|
||||
AttrDef(AttrDef),
|
||||
|
@ -1820,10 +1833,10 @@ pub enum Expr {
|
|||
Import(Accessor),
|
||||
}
|
||||
|
||||
impl_nested_display_for_chunk_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Decl, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
|
||||
impl_nested_display_for_chunk_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
|
||||
impl_display_from_nested!(Expr);
|
||||
impl_locational_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Decl, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
|
||||
impl_t_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Decl, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
|
||||
impl_locational_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
|
||||
impl_t_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
|
||||
|
||||
impl Default for Expr {
|
||||
fn default() -> Self {
|
||||
|
|
|
@ -1,4 +1,13 @@
|
|||
.StringIO!: ClassType
|
||||
.StringIO!.getvalue!: (self: Ref(StringIO!),) => Str
|
||||
.StringIO! <: FileLike!
|
||||
.StringIO!.read!: (self: RefMut(.StringIO!), ) => Str
|
||||
.StringIO!.write!: (self: RefMut(.StringIO!), s: Str) => NoneType
|
||||
.StringIO!.getvalue!: (self: Ref(.StringIO!),) => Str
|
||||
|
||||
.TextIOWrapper!: ClassType
|
||||
|
||||
.BytesIO!: ClassType
|
||||
.BytesIO! <: FileLike!
|
||||
.BytesIO!.read!: (self: RefMut(.BytesIO!), ) => Bytes
|
||||
.BytesIO!.write!: (self: RefMut(.BytesIO!), b: Bytes) => NoneType
|
||||
.newBytesIO = 'BytesIO': (bytes: Bytes,) -> .BytesIO!
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
.ZipFile!: ClassType
|
||||
.ZipFile! <: FileLike!
|
||||
|
||||
.ZipFile!.open!: (path: PathLike or NoneType := NoneType, mode := Str) => .ZipFile!
|
||||
.ZipFile!.open!: (path: PathLike or FileLike!, mode := Str) => .ZipFile!
|
||||
.ZipFile!.add!: (self: RefMut(.ZipFile!), name: PathLike, arcname: PathLike or NoneType := NoneType, recursive := Bool) => NoneType
|
||||
.ZipFile!.close!: (self: .ZipFile!,) => NoneType
|
||||
.ZipFile!.extractall!: (self: RefMut(.ZipFile!), path := PathLike, members: [Str; _] or NoneType := NoneType, numeric_owner := Bool) => NoneType
|
||||
|
|
|
@ -121,7 +121,6 @@ impl<'a> Linker<'a> {
|
|||
self.resolve_pymod_path(&mut arg.expr);
|
||||
}
|
||||
}
|
||||
Expr::Decl(_decl) => {}
|
||||
Expr::Def(def) => {
|
||||
for chunk in def.body.block.iter_mut() {
|
||||
self.resolve_pymod_path(chunk);
|
||||
|
@ -238,7 +237,6 @@ impl<'a> Linker<'a> {
|
|||
}
|
||||
}
|
||||
},
|
||||
Expr::Decl(_decl) => {}
|
||||
Expr::Def(def) => {
|
||||
for chunk in def.body.block.iter_mut() {
|
||||
self.replace_import(chunk);
|
||||
|
|
|
@ -1515,9 +1515,11 @@ impl ASTLowerer {
|
|||
_ => unreachable!(),
|
||||
};
|
||||
let id = body.id;
|
||||
self.ctx.assign_var_sig(&sig, found_body_t, id, py_name)?;
|
||||
self.ctx
|
||||
.assign_var_sig(&sig, found_body_t, id, py_name.clone())?;
|
||||
let mut ident = hir::Identifier::bare(ident.dot.clone(), ident.name.clone());
|
||||
ident.vi.t = found_body_t.clone();
|
||||
ident.vi.py_name = py_name;
|
||||
let sig = hir::VarSignature::new(ident);
|
||||
let body = hir::DefBody::new(body.op, block, body.id);
|
||||
Ok(hir::Def::new(hir::Signature::Var(sig), body))
|
||||
|
@ -1648,6 +1650,10 @@ impl ASTLowerer {
|
|||
t: &Type,
|
||||
py_name: Str,
|
||||
) -> LowerResult<()> {
|
||||
// .X = 'x': Type
|
||||
if ident.is_raw() {
|
||||
return Ok(());
|
||||
}
|
||||
if ident.is_const() {
|
||||
let vi = VarInfo::new(
|
||||
t.clone(),
|
||||
|
@ -1693,6 +1699,9 @@ impl ASTLowerer {
|
|||
}
|
||||
|
||||
fn declare_subtype(&mut self, ident: &ast::Identifier, trait_: &Type) -> LowerResult<()> {
|
||||
if ident.is_raw() {
|
||||
return Ok(());
|
||||
}
|
||||
if let Some((_, ctx)) = self.ctx.get_mut_type(ident.inspect()) {
|
||||
ctx.register_marker_trait(trait_.clone());
|
||||
Ok(())
|
||||
|
|
|
@ -130,8 +130,8 @@ impl fmt::Display for VarInfo {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"VarInfo{{t: {}, muty: {:?}, vis: {:?}, kind: {:?}}}",
|
||||
self.t, self.muty, self.vis, self.kind
|
||||
"VarInfo{{t: {}, muty: {:?}, vis: {:?}, kind: {:?}, py_name: {:?}}}",
|
||||
self.t, self.muty, self.vis, self.kind, self.py_name,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2018,6 +2018,10 @@ impl VarName {
|
|||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn is_raw(&self) -> bool {
|
||||
self.0.content.starts_with('\'')
|
||||
}
|
||||
|
||||
pub const fn token(&self) -> &Token {
|
||||
&self.0
|
||||
}
|
||||
|
@ -2094,6 +2098,10 @@ impl Identifier {
|
|||
self.name.is_const()
|
||||
}
|
||||
|
||||
pub fn is_raw(&self) -> bool {
|
||||
self.name.is_raw()
|
||||
}
|
||||
|
||||
pub const fn vis(&self) -> Visibility {
|
||||
match &self.dot {
|
||||
Some(_) => Visibility::Public,
|
||||
|
|
|
@ -795,6 +795,45 @@ impl Lexer /*<'a>*/ {
|
|||
))
|
||||
}
|
||||
|
||||
fn lex_raw_ident(&mut self) -> LexResult<Token> {
|
||||
let mut s = "\'".to_string();
|
||||
while let Some(c) = self.peek_cur_ch() {
|
||||
match c {
|
||||
'\n' => {
|
||||
let token = self.emit_token(Illegal, &s);
|
||||
return Err(LexError::simple_syntax_error(line!() as usize, token.loc()));
|
||||
}
|
||||
'\'' => {
|
||||
s.push(self.consume().unwrap());
|
||||
if self.peek_cur_ch() == Some('!') {
|
||||
s.push(self.consume().unwrap());
|
||||
}
|
||||
let token = self.emit_token(Symbol, &s);
|
||||
return Ok(token);
|
||||
}
|
||||
_ => {
|
||||
let c = self.consume().unwrap();
|
||||
s.push(c);
|
||||
if Self::is_bidi(c) {
|
||||
return Err(self._invalid_unicode_character(&s));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let token = self.emit_token(Illegal, &s);
|
||||
Err(LexError::syntax_error(
|
||||
0,
|
||||
token.loc(),
|
||||
switch_lang!(
|
||||
"japanese" => "raw識別子が'によって閉じられていません",
|
||||
"simplified_chinese" => "raw标识符没有被'关闭",
|
||||
"traditional_chinese" => "raw標誌符沒有被'關閉",
|
||||
"english" => "raw identifier is not closed by '",
|
||||
),
|
||||
None,
|
||||
))
|
||||
}
|
||||
|
||||
// for single strings and multi-line strings
|
||||
fn _invalid_unicode_character(&mut self, s: &str) -> LexError {
|
||||
let token = self.emit_token(Illegal, s);
|
||||
|
@ -1117,7 +1156,34 @@ impl Iterator for Lexer /*<'a>*/ {
|
|||
}
|
||||
}
|
||||
// TODO:
|
||||
Some('\'') => self.deny_feature("'", "raw identifier"),
|
||||
Some('\'') => {
|
||||
let c = self.peek_cur_ch();
|
||||
match c {
|
||||
None => {
|
||||
let token = self.emit_token(Illegal, "'");
|
||||
Some(Err(LexError::syntax_error(
|
||||
0,
|
||||
token.loc(),
|
||||
switch_lang!(
|
||||
"japanese" => "raw識別子が'によって閉じられていません",
|
||||
"simplified_chinese" => "raw識別子没被'关闭",
|
||||
"traditional_chinese" => "raw識別字沒被'關閉",
|
||||
"english" => "raw identifier is not ended with '",
|
||||
),
|
||||
None,
|
||||
)))
|
||||
}
|
||||
Some(c) => {
|
||||
if c == '\'' {
|
||||
self.consume(); // consume second '\''
|
||||
let token = self.emit_token(Illegal, "\"\"");
|
||||
Some(Err(LexError::simple_syntax_error(0, token.loc())))
|
||||
} else {
|
||||
Some(self.lex_raw_ident())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Symbolized operators (シンボル化された演算子)
|
||||
// e.g. `-`(l, r) = l + (-r)
|
||||
Some('`') => {
|
||||
|
|
6
examples/raw_ident.er
Normal file
6
examples/raw_ident.er
Normal file
|
@ -0,0 +1,6 @@
|
|||
t = 1
|
||||
'2t+3' = 2 * t + 3
|
||||
assert '2t+3' == 5
|
||||
|
||||
'echo'! = print!
|
||||
# 'echo' = print! is not permitted
|
|
@ -82,6 +82,11 @@ fn exec_quantified() -> Result<(), ()> {
|
|||
expect_success("examples/quantified.er")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exec_raw_ident() -> Result<(), ()> {
|
||||
expect_success("examples/raw_ident.er")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exec_rec() -> Result<(), ()> {
|
||||
// this script is valid but the current code generating process has a bug.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue