mirror of
https://github.com/typst/typst
synced 2025-08-20 01:48:34 +08:00
Compare commits
4 Commits
ed7b331862
...
78c4e5d5e6
Author | SHA1 | Date | |
---|---|---|---|
|
78c4e5d5e6 | ||
|
36ecbb2c8d | ||
|
51ab5b815c | ||
|
39b3d94bee |
@ -85,6 +85,10 @@ impl Eval for ast::ModuleImport<'_> {
|
|||||||
source_span, "module name would not be a valid identifier";
|
source_span, "module name would not be a valid identifier";
|
||||||
hint: "you can rename the import with `as`",
|
hint: "you can rename the import with `as`",
|
||||||
),
|
),
|
||||||
|
Err(BareImportError::Keyword(name)) => bail!(
|
||||||
|
source_span, "module `{name}` would be overridden as a keyword";
|
||||||
|
hint: "you can rename the import with `as`",
|
||||||
|
),
|
||||||
// Bad package spec would have failed the import already.
|
// Bad package spec would have failed the import already.
|
||||||
Err(BareImportError::PackageInvalid) => unreachable!(),
|
Err(BareImportError::PackageInvalid) => unreachable!(),
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ use crate::foundations::{
|
|||||||
///
|
///
|
||||||
/// You can call a function by writing a comma-separated list of function
|
/// You can call a function by writing a comma-separated list of function
|
||||||
/// _arguments_ enclosed in parentheses directly after the function name.
|
/// _arguments_ enclosed in parentheses directly after the function name.
|
||||||
/// Additionally, you can pass any number of trailing content blocks arguments
|
/// Additionally, you can pass any number of trailing content block arguments
|
||||||
/// to a function _after_ the normal argument list. If the normal argument list
|
/// to a function _after_ the normal argument list. If the normal argument list
|
||||||
/// would become empty, it can be omitted. Typst supports positional and named
|
/// would become empty, it can be omitted. Typst supports positional and named
|
||||||
/// arguments. The former are identified by position and type, while the latter
|
/// arguments. The former are identified by position and type, while the latter
|
||||||
|
@ -86,7 +86,7 @@ use ecow::EcoString;
|
|||||||
use unscanny::Scanner;
|
use unscanny::Scanner;
|
||||||
|
|
||||||
use crate::package::PackageSpec;
|
use crate::package::PackageSpec;
|
||||||
use crate::{is_ident, is_newline, Span, SyntaxKind, SyntaxNode};
|
use crate::{is_ident, is_keyword, is_newline, Span, SyntaxKind, SyntaxNode};
|
||||||
|
|
||||||
/// A typed AST node.
|
/// A typed AST node.
|
||||||
pub trait AstNode<'a>: Sized {
|
pub trait AstNode<'a>: Sized {
|
||||||
@ -2225,6 +2225,10 @@ impl<'a> ModuleImport<'a> {
|
|||||||
return Err(BareImportError::PathInvalid);
|
return Err(BareImportError::PathInvalid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if is_keyword(&name) {
|
||||||
|
return Err(BareImportError::Keyword(name));
|
||||||
|
}
|
||||||
|
|
||||||
Ok(name)
|
Ok(name)
|
||||||
}
|
}
|
||||||
_ => Err(BareImportError::Dynamic),
|
_ => Err(BareImportError::Dynamic),
|
||||||
@ -2242,13 +2246,15 @@ impl<'a> ModuleImport<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Reasons why a bare name cannot be determined for an import source.
|
/// Reasons why a bare name cannot be determined for an import source.
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
||||||
pub enum BareImportError {
|
pub enum BareImportError {
|
||||||
/// There is no statically resolvable binding name.
|
/// There is no statically resolvable binding name.
|
||||||
Dynamic,
|
Dynamic,
|
||||||
/// The import source is not a valid path or the path stem not a valid
|
/// The import source is not a valid path or the path stem not a valid
|
||||||
/// identifier.
|
/// identifier.
|
||||||
PathInvalid,
|
PathInvalid,
|
||||||
|
/// The import source is a valid path but the path stem is a keyword.
|
||||||
|
Keyword(EcoString),
|
||||||
/// The import source is not a valid package spec.
|
/// The import source is not a valid package spec.
|
||||||
PackageInvalid,
|
PackageInvalid,
|
||||||
}
|
}
|
||||||
|
@ -1063,6 +1063,12 @@ pub fn is_ident(string: &str) -> bool {
|
|||||||
.is_some_and(|c| is_id_start(c) && chars.all(is_id_continue))
|
.is_some_and(|c| is_id_start(c) && chars.all(is_id_continue))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether a string is a Typst keyword.
|
||||||
|
#[inline]
|
||||||
|
pub fn is_keyword(string: &str) -> bool {
|
||||||
|
keyword(string).is_some()
|
||||||
|
}
|
||||||
|
|
||||||
/// Whether a character can start an identifier.
|
/// Whether a character can start an identifier.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_id_start(c: char) -> bool {
|
pub fn is_id_start(c: char) -> bool {
|
||||||
|
@ -20,8 +20,8 @@ pub use self::file::FileId;
|
|||||||
pub use self::highlight::{highlight, highlight_html, Tag};
|
pub use self::highlight::{highlight, highlight_html, Tag};
|
||||||
pub use self::kind::SyntaxKind;
|
pub use self::kind::SyntaxKind;
|
||||||
pub use self::lexer::{
|
pub use self::lexer::{
|
||||||
is_id_continue, is_id_start, is_ident, is_newline, is_valid_label_literal_id,
|
is_id_continue, is_id_start, is_ident, is_keyword, is_newline,
|
||||||
link_prefix, split_newlines,
|
is_valid_label_literal_id, link_prefix, split_newlines,
|
||||||
};
|
};
|
||||||
pub use self::lines::Lines;
|
pub use self::lines::Lines;
|
||||||
pub use self::node::{LinkedChildren, LinkedNode, Side, SyntaxError, SyntaxNode};
|
pub use self::node::{LinkedChildren, LinkedNode, Side, SyntaxError, SyntaxNode};
|
||||||
|
@ -205,7 +205,7 @@
|
|||||||
single or double quotes.
|
single or double quotes.
|
||||||
|
|
||||||
The value is always of type [string]($str). More complex data
|
The value is always of type [string]($str). More complex data
|
||||||
may be parsed manually using functions like [`json.decode`]($json.decode).
|
may be parsed manually using functions like [`json`]($json).
|
||||||
|
|
||||||
- name: sym
|
- name: sym
|
||||||
title: General
|
title: General
|
||||||
|
@ -483,3 +483,8 @@ This is never reached.
|
|||||||
--- import-from-file-package-lookalike ---
|
--- import-from-file-package-lookalike ---
|
||||||
// Error: 9-28 file not found (searched at tests/suite/scripting/#test/mypkg:1.0.0)
|
// Error: 9-28 file not found (searched at tests/suite/scripting/#test/mypkg:1.0.0)
|
||||||
#import "#test/mypkg:1.0.0": *
|
#import "#test/mypkg:1.0.0": *
|
||||||
|
|
||||||
|
--- import-keyword ---
|
||||||
|
// Error: 9-26 module `set` would be overridden as a keyword
|
||||||
|
// Hint: 9-26 you can rename the import with `as`
|
||||||
|
#import "modules/set.typ"
|
||||||
|
1
tests/suite/scripting/modules/set.typ
Normal file
1
tests/suite/scripting/modules/set.typ
Normal file
@ -0,0 +1 @@
|
|||||||
|
// SKIP
|
Loading…
x
Reference in New Issue
Block a user