///|
/// Control lines retain raw arguments so a caller can inspect unsupported requests.
pub(all) enum LineKind {
Blank
Text(String)
Request(String, String)
Comment
} derive(Eq, Debug)
///|
pub(all) struct SourceLine {
kind : LineKind
span : Span
} derive(Eq, Debug)
///|
pub extend LineKind with Eq::{equal, not_equal}
///|
pub extend LineKind with @debug.Debug::{to_repr}
///|
pub extend SourceLine with Eq::{equal, not_equal}
///|
pub extend SourceLine with @debug.Debug::{to_repr}
///|
fn chars_string(chars : Array[Char], start : Int32, end : Int32) -> String {
let out = StringBuilder()
for i = start; i < end; i = i + 1 {
out.write_char(chars[i])
}
out.to_string()
}
///|
fn horizontal(c : Char) -> Bool {
c == ' ' || c == '\t'
}
///|
/// Scan without interpreting or executing any request. CRLF is normalized.
pub fn scan(
source : String,
options~ : Options = Options::default(),
) -> Result[Array[SourceLine], Diagnostic] {
if options.validate() is Some(d) {
return Err(d)
}
let mut total = 0
for _ in source.iter() {
total = total + 1
if total > options.max_chars {
return Err(
diagnostic("input-limit", "Source exceeds max_chars", 1, severity=Error),
)
}
}
let result : Array[SourceLine] = []
let mut line = 0
for raw in source.split("\n") {
line = line + 1
if line > options.max_lines {
return Err(
diagnostic(
"line-limit",
"Source exceeds max_lines",
line,
severity=Error,
),
)
}
let text = raw.to_owned().trim_end(chars="\r").to_owned()
let chars = text.to_array()
if chars.length() > options.max_line_chars {
return Err(
diagnostic(
"line-length-limit",
"Source line exceeds max_line_chars",
line,
severity=Error,
),
)
}
let span = Span::line_span(line, chars.length())
let kind = if chars.is_empty() {
Blank
} else if chars[0] == '.' || chars[0] == '\'' {
let mut i = 1
while i < chars.length() && horizontal(chars[i]) {
i = i + 1
}
let start = i
while i < chars.length() && !horizontal(chars[i]) {
i = i + 1
}
let name = chars_string(chars, start, i)
while i < chars.length() && horizontal(chars[i]) {
i = i + 1
}
if name.has_prefix("\\\"") || name.has_prefix("\\#") {
Comment
} else {
Request(name, chars_string(chars, i, chars.length()))
}
} else {
Text(text)
}
result.push({ kind, span, })
}
Ok(result)
}