Skip to content

Commit

Permalink
rename Language API to Parser (NomicFoundation#1117)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik authored Oct 1, 2024
1 parent 5bb3558 commit be7bb79
Show file tree
Hide file tree
Showing 55 changed files with 264 additions and 259 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-timers-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": minor
---

rename `Language` API to `Parser`, in preparation for introducing a multi-file compilation API.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::fs;
use std::path::PathBuf;

use clap::Parser;
use semver::Version;

use crate::diagnostic;
use crate::parser::Language;
use crate::parser::Parser;

#[derive(Parser, Debug)]
#[derive(clap::Parser, Debug)]
pub struct ParseCommand {
/// File path to the source file to parse
file_path: PathBuf,
Expand All @@ -34,8 +33,8 @@ impl ParseCommand {
.unwrap_or_else(|_| panic!("File not found: {file_path:?}"));

let input = fs::read_to_string(&file_path).unwrap();
let language = Language::new(version).unwrap();
let parse_output = language.parse(Language::ROOT_KIND, &input);
let parser = Parser::new(version).unwrap();
let parse_output = parser.parse(Parser::ROOT_KIND, &input);

if !parse_output.is_valid() {
const COLOR: bool = true;
Expand Down

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

7 changes: 4 additions & 3 deletions crates/codegen/runtime/cargo/src/runtime/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#[path = "generated/language.rs"]
mod language;
mod lexer;
mod parse_error;
mod parse_output;
#[allow(clippy::module_inception)]
#[path = "generated/parser.rs"]
mod parser;
mod parser_support;
mod scanner_macros;

pub use language::{Language, LanguageInitializationError};
pub use parse_error::ParseError;
pub use parse_output::ParseOutput;
pub use parser::{Parser, ParserInitializationError};
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::parser::ParseOutput;

#[derive(Debug)]
#[cfg_attr(feature = "__private_napi_interfaces", napi(namespace = "parser"))]
pub struct Language {
pub struct Parser {
{%- if not rendering_in_stubs -%}
{%- for version in model.breaking_language_versions -%}
{% if loop.first %} {# The first supported version may not be referenced by the items #}
Expand All @@ -46,7 +46,7 @@ pub struct Language {
}

#[derive(thiserror::Error, Debug)]
pub enum LanguageInitializationError {
pub enum ParserInitializationError {
#[error("Unsupported language version '{0}'.")]
UnsupportedLanguageVersion(Version),

Expand All @@ -56,13 +56,13 @@ pub enum LanguageInitializationError {
}

#[cfg(feature = "__private_napi_interfaces")]
impl From<LanguageInitializationError> for napi::Error {
fn from(value: LanguageInitializationError) -> Self {
impl From<ParserInitializationError> for napi::Error {
fn from(value: ParserInitializationError) -> Self {
napi::Error::from_reason(value.to_string())
}
}

impl Language {
impl Parser {
pub const SUPPORTED_VERSIONS: &'static [Version] = &[
{%- if not rendering_in_stubs -%}
{% for version in model.all_language_versions %}
Expand All @@ -73,7 +73,7 @@ impl Language {

pub const ROOT_KIND: NonterminalKind = NonterminalKind::{{ model.kinds.root_kind }};

pub fn new(version: Version) -> std::result::Result<Self, LanguageInitializationError> {
pub fn new(version: Version) -> std::result::Result<Self, ParserInitializationError> {
if Self::SUPPORTED_VERSIONS.binary_search(&version).is_ok() {
Ok(Self {
{%- if not rendering_in_stubs -%}
Expand All @@ -85,7 +85,7 @@ impl Language {
version,
})
} else {
Err(LanguageInitializationError::UnsupportedLanguageVersion(version))
Err(ParserInitializationError::UnsupportedLanguageVersion(version))
}
}

Expand Down Expand Up @@ -138,20 +138,20 @@ impl Language {
}
}

impl Lexer for Language {
impl Lexer for Parser {
fn leading_trivia(&self, input: &mut ParserContext<'_>) -> ParserResult {
{%- if rendering_in_stubs -%}
unreachable!("Invoking leading_trivia in stubs: {input:#?}")
{%- else -%}
Language::leading_trivia(self, input)
Parser::leading_trivia(self, input)
{%- endif -%}
}

fn trailing_trivia(&self, input: &mut ParserContext<'_>) -> ParserResult {
{%- if rendering_in_stubs -%}
unreachable!("Invoking trailing_trivia in stubs: {input:#?}")
{%- else -%}
Language::trailing_trivia(self, input)
Parser::trailing_trivia(self, input)
{%- endif -%}
}

Expand Down Expand Up @@ -283,11 +283,11 @@ impl Lexer for Language {
// NAPI-exposed functions have to accept owned values.
#[allow(clippy::needless_pass_by_value)]
#[napi(namespace = "parser")]
impl Language {
impl Parser {

#[napi(constructor, catch_unwind)]
pub fn new_napi(version: String) -> std::result::Result<Self, napi::Error> {
let version = Version::parse(&version).map_err(|_| LanguageInitializationError::InvalidSemanticVersion(version))?;
let version = Version::parse(&version).map_err(|_| ParserInitializationError::InvalidSemanticVersion(version))?;
Self::new(version).map_err(|e| e.into())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ use crate::parser::parser_support::parser_result::{
};
use crate::parser::{ParseError, ParseOutput};

pub trait ParserFunction<L>
pub trait ParserFunction<P>
where
Self: Fn(&L, &mut ParserContext<'_>) -> ParserResult,
Self: Fn(&P, &mut ParserContext<'_>) -> ParserResult,
{
fn parse(&self, language: &L, input: &str) -> ParseOutput;
fn parse(&self, parser: &P, input: &str) -> ParseOutput;
}

impl<L, F> ParserFunction<L> for F
impl<P, F> ParserFunction<P> for F
where
L: Lexer,
F: Fn(&L, &mut ParserContext<'_>) -> ParserResult,
P: Lexer,
F: Fn(&P, &mut ParserContext<'_>) -> ParserResult,
{
#[allow(clippy::too_many_lines)]
fn parse(&self, language: &L, input: &str) -> ParseOutput {
fn parse(&self, parser: &P, input: &str) -> ParseOutput {
let mut stream = ParserContext::new(input);
let mut result = self(language, &mut stream);
let mut result = self(parser, &mut stream);

// For a succesful/recovered parse, collect any remaining trivia as part of the parse result
if let ParserResult::Match(r#match) = &mut result {
Expand All @@ -33,7 +33,7 @@ where
)
};

let eof_trivia = match Lexer::leading_trivia(language, &mut stream) {
let eof_trivia = match Lexer::leading_trivia(parser, &mut stream) {
ParserResult::Match(eof_trivia) if !eof_trivia.nodes.is_empty() => {
Some(eof_trivia.nodes)
}
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ interface parser {
use cst.{cursor, node, nonterminal-kind, text-range};
use diagnostic.{severity};

resource language {
resource parser {
supported-versions: static func() -> list<string>;
new: static func(version: string) -> result<language, string>;
new: static func(version: string) -> result<parser, string>;
version: func() -> string;
parse: func(kind: nonterminal-kind, input: string) -> parse-output;
}
Expand Down
18 changes: 9 additions & 9 deletions crates/codegen/runtime/cargo/src/runtime/wit/wrappers/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ mod ffi {
};
pub use crate::wit::interface::exports::nomic_foundation::slang::diagnostic::Severity;
pub use crate::wit::interface::exports::nomic_foundation::slang::parser::{
Guest, GuestLanguage, GuestParseError, GuestParseOutput, Language, LanguageBorrow,
NonterminalKind, ParseError, ParseErrorBorrow, ParseOutput, ParseOutputBorrow,
Guest, GuestParseError, GuestParseOutput, GuestParser, NonterminalKind, ParseError,
ParseErrorBorrow, ParseOutput, ParseOutputBorrow, Parser, ParserBorrow,
};
}

mod rust {
pub use crate::parser::{Language, ParseError, ParseOutput};
pub use crate::parser::{ParseError, ParseOutput, Parser};
}

impl ffi::Guest for crate::wit::World {
type Language = LanguageWrapper;
type Parser = ParserWrapper;
type ParseError = ParseErrorWrapper;
type ParseOutput = ParseOutputWrapper;
}

//================================================
//
// resource language
// resource parser
//
//================================================

define_wrapper! { Language {
fn new(version: String) -> Result<ffi::Language, String> {
define_wrapper! { Parser {
fn new(version: String) -> Result<ffi::Parser, String> {
semver::Version::parse(&version)
.map_err(|_| format!("Invalid version: {version}"))
.and_then(|version| rust::Language::new(version).map_err(|e| e.to_string()))
.and_then(|version| rust::Parser::new(version).map_err(|e| e.to_string()))
.map(IntoFFI::_into_ffi)
}

Expand All @@ -41,7 +41,7 @@ define_wrapper! { Language {
}

fn supported_versions() -> Vec<String> {
rust::Language::SUPPORTED_VERSIONS
rust::Parser::SUPPORTED_VERSIONS
.iter()
.map(|v| v.to_string())
.collect()
Expand Down

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

4 changes: 2 additions & 2 deletions crates/codegen/runtime/npm/src/runtime/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as generated from "../napi-bindings/generated";
import { DiagnosticInterface } from "../diagnostic";

export const Language = generated.parser.Language;
export type Language = generated.parser.Language;
export const Parser = generated.parser.Parser;
export type Parser = generated.parser.Parser;

export const ParseError = generated.parser.ParseError;
export type ParseError = generated.parser.ParseError;
Expand Down

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

Loading

0 comments on commit be7bb79

Please sign in to comment.