Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion design/wasi_unstable/typenames.witx
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@
)

;; Identifiers for preopened capabilities.
(typename preopentype_t
(typename $preopentype_t
(enum u8
;; A pre-opened directory.
$PREOPENTYPE_DIR
Expand Down
2 changes: 2 additions & 0 deletions tools/witx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
Cargo.lock
21 changes: 21 additions & 0 deletions tools/witx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "witx"
version = "0.1.0"
description = "Parse and validate witx file format"
homepage = "https://github.com/WebAssembly/WASI"
repository = "https://github.com/WebAssembly/WASI"
license = "Apache-2.0"
categories = ["wasm"]
authors = ["Pat Hickey <phickey@fastly.com>"]
edition = "2018"

[lib]
crate-type=["rlib"]

[[bin]]
name = "witx"
path = "src/main.rs"

[dependencies]
clap = "2"
failure = "0.1"
13 changes: 13 additions & 0 deletions tools/witx/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2019 WebAssembly Community Group participants

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
163 changes: 163 additions & 0 deletions tools/witx/src/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#![allow(dead_code)]
use std::collections::HashMap;
use std::rc::{Rc, Weak};

pub use crate::parser::BuiltinType;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Id(String);

impl Id {
pub fn new<S: AsRef<str>>(s: S) -> Self {
Id(s.as_ref().to_string())
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}

#[derive(Debug, Clone)]
pub struct Document {
pub definitions: Vec<Definition>,
pub entries: HashMap<Id, Entry>,
}

#[derive(Debug, Clone)]
pub enum Definition {
Datatype(Rc<Datatype>),
Module(Rc<Module>),
}

#[derive(Debug, Clone)]
pub enum Entry {
Datatype(Weak<Datatype>),
Module(Weak<Module>),
}

impl Entry {
pub fn kind(&self) -> &'static str {
match self {
Entry::Datatype { .. } => "datatype",
Entry::Module { .. } => "module",
}
}
}

#[derive(Debug, Clone)]
pub enum DatatypeIdent {
Builtin(BuiltinType),
Array(Box<DatatypeIdent>),
Pointer(Box<DatatypeIdent>),
ConstPointer(Box<DatatypeIdent>),
Ident(Rc<Datatype>),
}

#[derive(Debug, Clone)]
pub struct Datatype {
pub name: Id,
pub variant: DatatypeVariant,
}

#[derive(Debug, Clone)]
pub enum DatatypeVariant {
Alias(AliasDatatype),
Enum(EnumDatatype),
Flags(FlagsDatatype),
Struct(StructDatatype),
Union(UnionDatatype),
}

#[derive(Debug, Clone)]
pub struct AliasDatatype {
pub name: Id,
pub to: DatatypeIdent,
}

#[derive(Debug, Clone)]
pub enum IntRepr {
I8,
I16,
I32,
I64,
}

#[derive(Debug, Clone)]
pub struct EnumDatatype {
pub name: Id,
pub repr: IntRepr,
pub variants: Vec<Id>,
}

#[derive(Debug, Clone)]
pub struct FlagsDatatype {
pub name: Id,
pub repr: IntRepr,
pub flags: Vec<Id>,
}

#[derive(Debug, Clone)]
pub struct StructDatatype {
pub name: Id,
pub members: Vec<StructMember>,
}

#[derive(Debug, Clone)]
pub struct StructMember {
pub name: Id,
pub type_: DatatypeIdent,
}

#[derive(Debug, Clone)]
pub struct UnionDatatype {
pub name: Id,
pub variants: Vec<UnionVariant>,
}

#[derive(Debug, Clone)]
pub struct UnionVariant {
pub name: Id,
pub type_: DatatypeIdent,
}

#[derive(Debug, Clone)]
pub struct Module {
pub name: Id,
pub definitions: Vec<ModuleDefinition>,
pub entries: HashMap<Id, ModuleEntry>,
}

#[derive(Debug, Clone)]
pub enum ModuleDefinition {
Import(Rc<ModuleImport>),
Func(Rc<InterfaceFunc>),
}

#[derive(Debug, Clone)]
pub enum ModuleEntry {
Import(Weak<ModuleImport>),
Func(Weak<InterfaceFunc>),
}

#[derive(Debug, Clone)]
pub struct ModuleImport {
pub name: Id,
pub variant: ModuleImportVariant,
}

#[derive(Debug, Clone)]
pub enum ModuleImportVariant {
Memory,
}

#[derive(Debug, Clone)]
pub struct InterfaceFunc {
pub name: Id,
pub params: Vec<InterfaceFuncParam>,
pub results: Vec<InterfaceFuncParam>,
}

#[derive(Debug, Clone)]
pub struct InterfaceFuncParam {
pub name: Id,
pub type_: DatatypeIdent,
}
Loading