Skip to main content

weavatrix_parse/syntax/
mod.rs

1//! Lexical shape of each supported language.
2//!
3//! Languages differ in a small number of lexical decisions - how a comment
4//! starts, which quotes open a string, whether a backslash escapes, whether
5//! indentation is significant - and agree on everything else. Describing those
6//! differences as data keeps one tokenizer correct for all of them instead of
7//! one hand-written scanner per language.
8
9/// A language this crate can tokenize.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11#[non_exhaustive]
12pub enum Language {
13    JavaScript,
14    TypeScript,
15    Graphql,
16    Protobuf,
17    Rust,
18    Python,
19    Go,
20    Java,
21    CSharp,
22    C,
23    Cpp,
24    Sql,
25    Solidity,
26    Swift,
27    Terraform,
28    Html,
29    Xml,
30    Markdown,
31    /// Markdown with JavaScript imports and components.
32    Mdx,
33    ReStructuredText,
34    AsciiDoc,
35    Css,
36    /// SCSS, Sass and Less, which differ from CSS by allowing `//` comments
37    /// and nesting selectors.
38    Scss,
39    Bash,
40    Yaml,
41}
42
43mod detection;
44mod profiles;
45
46/// The lexical rules of one language.
47///
48/// The flags are independent lexical facts rather than a state machine, so
49/// they are listed plainly instead of being packed into an option type that
50/// would obscure which language has which behaviour.
51#[derive(Debug, Clone, Copy)]
52#[non_exhaustive]
53#[allow(clippy::struct_excessive_bools)]
54pub struct Syntax {
55    pub line_comments: &'static [&'static str],
56    pub block_comment: Option<(&'static str, &'static str)>,
57    pub nested_block_comments: bool,
58    pub quotes: &'static [char],
59    /// Quote that opens a string containing `${...}` expressions.
60    pub interpolated_quote: Option<char>,
61    pub escapes: bool,
62    /// Whether `/` can open a regular-expression literal.
63    pub regex_literals: bool,
64    /// Whether `r"..."` and `r#"..."#` forms exist.
65    pub raw_strings: bool,
66    /// Whether `"""..."""` spans lines.
67    pub triple_quotes: bool,
68    /// Whether `'` opens a character literal that a lifetime is also written
69    /// with. Rust needs this: `'a` is a lifetime and `'"'` is a quote
70    /// character, and treating `'` as an ordinary quote or as ordinary
71    /// punctuation gets one of the two wrong.
72    pub char_literals: bool,
73    pub significant_indentation: bool,
74    pub identifier_extra: &'static [char],
75}