1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
//! Parsing and AST types.
//!
//! The entrypoint is to invoke [`parse_kdl_script`][] which is implicitly
//! handled by [`Compiler::compile_path`][] or [`Compiler::compile_string`][]
//! and will produce a [`ParsedProgram`][].
//!
//! Things like name resolution are handled by the [type checker](`crate::types`).
use std::sync::Arc;
use kdl::{KdlDocument, KdlEntry, KdlNode};
use miette::{Diagnostic, NamedSource, SourceSpan};
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::{alpha1, alphanumeric1};
use nom::combinator::{all_consuming, cut, opt, recognize};
use nom::error::{context, VerboseError};
use nom::multi::{many0, many0_count, separated_list1};
use nom::sequence::{delimited, pair, preceded, separated_pair};
use nom::{Finish, IResult};
use thiserror::Error;
use tracing::trace;
use crate::spanned::Spanned;
use crate::{Compiler, Result};
/// A string that may refer to another item like a type of function
pub type Ident = Spanned<String>;
pub type StableMap<K, V> = linked_hash_map::LinkedHashMap<K, V>;
/// An error that occured during parsing
#[derive(Debug, Error, Diagnostic)]
#[error("{message}")]
pub struct KdlScriptParseError {
pub message: String,
#[source_code]
pub src: Arc<NamedSource>,
#[label]
pub span: SourceSpan,
#[help]
pub help: Option<String>,
}
/// A Parsed KDLScript program
#[derive(Debug, Clone)]
pub struct ParsedProgram {
/// The type definitions
pub tys: StableMap<Ident, TyDecl>,
/// The function definitions
pub funcs: StableMap<Ident, FuncDecl>,
/// Where in funcs builtins like `+` start (if at all).
pub builtin_funcs_start: usize,
}
/// A Type declaration
#[derive(Debug, Clone)]
pub enum TyDecl {
/// A Struct
Struct(StructDecl),
/// An untagged union
Union(UnionDecl),
/// A c-style enum
Enum(EnumDecl),
/// A tagged union (rust-style enum)
Tagged(TaggedDecl),
/// A transparent type alias
Alias(AliasDecl),
/// A type pun
Pun(PunDecl),
}
/// A type "name" (which may be structural like `[u32; 4]`).
///
/// It's like an ident but, for types -- a tydent!
#[derive(Debug, Clone)]
pub enum Tydent {
/// A named type (the type checker will resolve this)
Name(Ident),
/// A fixed length array
Array(Box<Spanned<Tydent>>, u64),
/// A by-reference type
Ref(Box<Spanned<Tydent>>),
/// The empty tuple -- `()`
Empty,
}
/// An attribute that can be hung off a function or type.
///
/// Currently stubbed out, not really used. Potential uses:
///
/// * setting the backing type on an Enum's tag
/// * packed(N)
/// * align(N)
/// * passthrough attrs to underlying language
///
/// Probably this should be broken up so that you can't "pack"
/// a function or other such nonsense...
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Attr {
/// rust-style derive (unused, just for testing)
Derive(AttrDerive),
/// The type should be packed
Packed(AttrPacked),
/// Pass this attribute through to the target language
Passthrough(AttrPassthrough),
}
/// An attribute declaring this type should be packed (remove padding/align).
///
/// TODO: add support for an integer argument for the max align of a
/// field? Without one the default is 1. I never see packed(2) or whatever
/// so I've never seriously thought through the implications...
///
/// @packed (N?)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AttrPacked {}
/// An attribute declaring a rust-style derive (unused, just for testing).
///
/// @derive "Trait1" "Trait2" ...
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AttrDerive(Vec<Spanned<String>>);
/// An attribute to passthrough to the target language.
///
/// @ "whatever you want buddy"
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AttrPassthrough(Spanned<String>);
/// A struct decl.
///
/// Field names may be positional by naming them underscore (`_`).
/// For languages like Rust, if all fields are declared like this
/// it should be lowered to a tuple-struct. Otherwise positional
/// fields will be autonamed something like field0, field1, ...
#[derive(Debug, Clone)]
pub struct StructDecl {
/// Name of the struct
pub name: Ident,
/// Fields
pub fields: Vec<TypedVar>,
/// Attributes
pub attrs: Vec<Attr>,
}
/// An untagged union decl.
///
/// Variant names may be positional by naming them underscore (`_`).
/// Positional variants will be autonamed something like Case0, Case1, ...
#[derive(Debug, Clone)]
pub struct UnionDecl {
/// Name of the union
pub name: Ident,
/// Fields (variants)
pub fields: Vec<TypedVar>,
pub attrs: Vec<Attr>,
}
/// A c-like enum decl.
///
/// Variant names may be positional by naming them underscore (`_`).
/// Positional variants will be autonamed something like Case0, Case1, ...
#[derive(Debug, Clone)]
pub struct EnumDecl {
pub name: Ident,
pub variants: Vec<EnumVariant>,
pub attrs: Vec<Attr>,
}
/// A variant of an [`EnumDecl`].
#[derive(Debug, Clone)]
pub struct EnumVariant {
pub name: Ident,
/// Optional value this case is required to have
/// in its underlying integer representation.
pub val: Option<IntExpr>,
}
/// A tagged union (rust-like enum).
///
/// Variant names may be positional by naming them underscore (`_`).
/// Positional variants will be autonamed something like Case0, Case1, ...
#[derive(Debug, Clone)]
pub struct TaggedDecl {
pub name: Ident,
pub variants: Vec<TaggedVariant>,
pub attrs: Vec<Attr>,
}
/// A variant of a [`TaggedDecl`][].
#[derive(Debug, Clone)]
pub struct TaggedVariant {
pub name: Ident,
pub fields: Option<Vec<TypedVar>>,
}
/// A type pun between different languages.
#[derive(Debug, Clone)]
pub struct PunDecl {
pub name: Ident,
pub blocks: Vec<PunBlock>,
pub attrs: Vec<Attr>,
}
/// A block for a [`PunDecl`][].
#[derive(Debug, Clone)]
pub struct PunBlock {
pub selector: PunSelector,
pub decl: TyDecl,
}
/// A selector for a [`PunBlock`][]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PunSelector {
/// Selector applies if any of the following apply.
Any(Vec<PunSelector>),
/// Selector applies if all of the following apply.
All(Vec<PunSelector>),
/// Selector applies if the [`PunEnv::lang`] is the following.
Lang(Spanned<String>),
/// Selector always applies (default fallback).
Default,
}
/// The environment required to resolve a [`PunSelector`][].
#[derive(Debug, Clone)]
pub struct PunEnv {
/// The target language
///
/// TODO: make this an enum?
pub lang: String,
// compiler: String,
// os: String,
// cpu: String,
}
impl PunSelector {
/// Check if this [`PunSelector`][] matches the given [`PunEnv`][].
pub fn matches(&self, env: &PunEnv) -> bool {
use PunSelector::*;
match self {
Any(args) => args.iter().any(|s| s.matches(env)),
All(args) => args.iter().all(|s| s.matches(env)),
Lang(lang) => env.lang == **lang,
Default => true,
}
}
}
/// A transparent type alias.
#[derive(Debug, Clone)]
pub struct AliasDecl {
pub name: Ident,
pub alias: Spanned<Tydent>,
pub attrs: Vec<Attr>,
}
/// A (name, type) pair that occurs in many places like field/arg decls.
#[derive(Debug, Clone)]
pub struct TypedVar {
pub name: Option<Ident>,
pub ty: Spanned<Tydent>,
}
/// A function declaration
#[derive(Debug, Clone)]
pub struct FuncDecl {
pub name: Ident,
pub inputs: Vec<TypedVar>,
pub outputs: Vec<TypedVar>,
pub attrs: Vec<Attr>,
#[cfg(feature = "eval")]
pub body: Vec<Stmt>,
}
/// The parser, used to hold onto some global state for things like diagnostic.
struct Parser<'a> {
// comp: &'a mut Compiler,
src: Arc<NamedSource>,
ast: &'a KdlDocument,
}
/// Parse a KdlScript program!
pub fn parse_kdl_script(
_comp: &mut Compiler,
src: Arc<NamedSource>,
ast: &KdlDocument,
) -> Result<ParsedProgram> {
let mut parser = Parser { src, ast };
parser.parse()
}
impl Parser<'_> {
/// Parse a KdlScript program!
fn parse(&mut self) -> Result<ParsedProgram> {
trace!("parsing");
let mut program = self.parse_module(self.ast)?;
#[cfg(feature = "eval")]
program.add_builtin_funcs()?;
Ok(program)
}
/// Parse a "module" which is either the entire program or the contents of a [`PunBlock`][].
fn parse_module(&mut self, doc: &KdlDocument) -> Result<ParsedProgram> {
let mut funcs = StableMap::new();
let mut tys = StableMap::new();
let mut cur_attrs = vec![];
for node in doc.nodes() {
let name = node.name().value();
// If it's an attribute, gather it up to be given to a "real" item
if name.starts_with('@') {
cur_attrs.push(self.attr(node)?);
continue;
}
// Ok it's a real item, grab all the attributes, they belong to it
let attrs = std::mem::take(&mut cur_attrs);
// Now parse the various kinds of top-level items
match name {
"fn" => {
let func = self.func_decl(node, attrs)?;
funcs.insert(func.name.clone(), func);
}
"struct" => {
let ty = self.struct_decl(node, attrs)?;
let old = tys.insert(ty.name.clone(), TyDecl::Struct(ty));
assert!(old.is_none(), "duplicate type def");
}
"union" => {
let ty = self.union_decl(node, attrs)?;
let old = tys.insert(ty.name.clone(), TyDecl::Union(ty));
assert!(old.is_none(), "duplicate type def");
}
"enum" => {
let ty = self.enum_decl(node, attrs)?;
let old = tys.insert(ty.name.clone(), TyDecl::Enum(ty));
assert!(old.is_none(), "duplicate type def");
}
"tagged" => {
let ty = self.tagged_decl(node, attrs)?;
let old = tys.insert(ty.name.clone(), TyDecl::Tagged(ty));
assert!(old.is_none(), "duplicate type def");
}
"alias" => {
let ty = self.alias_decl(node, attrs)?;
let old = tys.insert(ty.name.clone(), TyDecl::Alias(ty));
assert!(old.is_none(), "duplicate type def");
}
"pun" => {
let ty = self.pun_decl(node, attrs)?;
let old = tys.insert(ty.name.clone(), TyDecl::Pun(ty));
assert!(old.is_none(), "duplicate type def");
}
x => {
return Err(KdlScriptParseError {
message: format!("I don't know what a '{x}' is"),
src: self.src.clone(),
span: *node.name().span(),
help: None,
})?;
}
}
}
// Anything added after this point is a builtin!
let builtin_funcs_start = funcs.len();
Ok(ParsedProgram {
tys,
funcs,
builtin_funcs_start,
})
}
/// Parse a `struct` node.
fn struct_decl(&mut self, node: &KdlNode, attrs: Vec<Attr>) -> Result<StructDecl> {
trace!("struct decl");
let name = self.one_string(node, "type name")?;
let name = self.ident(name)?;
let fields = self.typed_var_children(node)?;
Ok(StructDecl {
name,
fields,
attrs,
})
}
/// Parse a `union` node.
fn union_decl(&mut self, node: &KdlNode, attrs: Vec<Attr>) -> Result<UnionDecl> {
trace!("union decl");
let name = self.one_string(node, "type name")?;
let name = self.ident(name)?;
let fields = self.typed_var_children(node)?;
Ok(UnionDecl {
name,
fields,
attrs,
})
}
/// Parse an `enum` node.
fn enum_decl(&mut self, node: &KdlNode, attrs: Vec<Attr>) -> Result<EnumDecl> {
trace!("enum decl");
let name = self.one_string(node, "type name")?;
let name = self.ident(name)?;
let variants = self.enum_variant_children(node)?;
Ok(EnumDecl {
name,
variants,
attrs,
})
}
/// Parse a `tagged` node.
fn tagged_decl(&mut self, node: &KdlNode, attrs: Vec<Attr>) -> Result<TaggedDecl> {
trace!("enum decl");
let name = self.one_string(node, "type name")?;
let name = self.ident(name)?;
let variants = self.tagged_variant_children(node)?;
Ok(TaggedDecl {
name,
variants,
attrs,
})
}
/// Parse a `pun` node.
fn pun_decl(&mut self, node: &KdlNode, attrs: Vec<Attr>) -> Result<PunDecl> {
let name = self.one_string(node, "type name")?;
let name = self.ident(name)?;
// Parse the pun blocks
let mut blocks = vec![];
for item in node.children().into_iter().flat_map(|d| d.nodes()) {
let item_name = item.name().value();
match item_name {
"lang" => {
let langs = self.string_list(item.entries())?;
if langs.is_empty() {
let node_ident = item.name().span();
let after_ident = node_ident.offset() + node_ident.len();
return Err(KdlScriptParseError {
message: "Hey I need a lang name (string) here!".to_string(),
src: self.src.clone(),
span: (after_ident..after_ident).into(),
help: None,
})?;
}
let final_ty = self.pun_block(item, &name)?;
blocks.push(PunBlock {
selector: PunSelector::Any(
langs.into_iter().map(PunSelector::Lang).collect(),
),
decl: final_ty,
});
}
"default" => {
self.no_args(item)?;
let final_ty = self.pun_block(item, &name)?;
blocks.push(PunBlock {
selector: PunSelector::Default,
decl: final_ty,
});
}
x => {
return Err(KdlScriptParseError {
message: format!("I don't know what a '{x}' is here"),
src: self.src.clone(),
span: *item.name().span(),
help: None,
})?;
}
}
}
Ok(PunDecl {
name,
blocks,
attrs,
})
}
/// Parse a pun block, expecting only a single type with the pun's name.
///
/// In the future we may allow [`PunBlock`][]s to declare other "private" types
/// that are only in scope for the block but required to define the final type.
/// For now we punt on this to avoid thinking through the name resolution implications
/// for things like name shadowing.
fn pun_block(&mut self, block: &KdlNode, final_ty_name: &Ident) -> Result<TyDecl> {
if let Some(doc) = block.children() {
// Recursively parse this block as an entire KdlScript program
let defs = self.parse_module(doc)?;
// Don't want any functions
if let Some((_name, func)) = defs.funcs.iter().next() {
return Err(KdlScriptParseError {
message: "puns can't contain function decls".to_string(),
src: self.src.clone(),
span: Spanned::span(&func.name),
help: None,
})?;
}
let mut final_ty = None;
// Only want one type declared (might loosen this later)
for (ty_name, ty) in defs.tys {
if &ty_name == final_ty_name {
// this is the type
final_ty = Some(ty);
} else {
return Err(KdlScriptParseError {
message: "pun declared a type other than what it should have".to_string(),
src: self.src.clone(),
span: Spanned::span(&ty_name),
help: None,
})?;
}
}
// Check that we defined the type
if let Some(ty) = final_ty {
Ok(ty)
} else {
Err(KdlScriptParseError {
message: "pun block failed to define the type it puns!".to_string(),
src: self.src.clone(),
span: *block.span(),
help: None,
})?
}
} else {
Err(KdlScriptParseError {
message: "pun blocks need bodies".to_string(),
src: self.src.clone(),
span: *block.span(),
help: None,
})?
}
}
/// Parse an `alias` node.
fn alias_decl(&mut self, node: &KdlNode, attrs: Vec<Attr>) -> Result<AliasDecl> {
let name = self.string_at(node, "type name", 0)?;
let name = self.ident(name)?;
let alias_str = self.string_at(node, "type name", 1)?;
let alias = self.tydent(&alias_str)?;
Ok(AliasDecl { name, alias, attrs })
}
/// Parse a `fn` node.
fn func_decl(&mut self, node: &KdlNode, attrs: Vec<Attr>) -> Result<FuncDecl> {
trace!("fn");
let name = self.one_string(node, "function name")?;
let name = self.ident(name)?;
let mut inputs = vec![];
let mut outputs = vec![];
#[cfg(feature = "eval")]
let mut body = vec![];
let mut reached_body = false;
let mut input_span = None;
let mut output_span = None;
for stmt in node.children().into_iter().flat_map(|d| d.nodes()) {
match stmt.name().value() {
"inputs" => {
trace!("fn input");
if reached_body {
return Err(KdlScriptParseError {
message: "input declaration must come before the body".to_string(),
src: self.src.clone(),
span: *stmt.name().span(),
help: None,
})?;
}
if let Some(_old_input) = input_span {
return Err(KdlScriptParseError {
message: "duplicate input block".to_string(),
src: self.src.clone(),
span: *stmt.name().span(),
help: None,
})?;
}
if let Some(_old_output) = output_span {
return Err(KdlScriptParseError {
message: "It's confusing to declare inputs after outputs".to_string(),
src: self.src.clone(),
span: *stmt.name().span(),
help: Some("Move this before the output block".to_string()),
})?;
}
self.no_args(stmt)?;
inputs = self.typed_var_children(stmt)?;
input_span = Some(*stmt.name().span());
continue;
}
"outputs" => {
trace!("fn output");
if reached_body {
return Err(KdlScriptParseError {
message: "output declaration must come before the body".to_string(),
src: self.src.clone(),
span: *stmt.name().span(),
help: None,
})?;
}
if let Some(_old_output) = output_span {
return Err(KdlScriptParseError {
message: "duplicate output block".to_string(),
src: self.src.clone(),
span: *stmt.name().span(),
help: None,
})?;
}
self.no_args(stmt)?;
outputs = self.typed_var_children(stmt)?;
output_span = Some(*stmt.name().span());
continue;
}
x => {
#[cfg(feature = "eval")]
match x {
"let" => {
trace!("let stmt");
let name = self.string_at(stmt, "variable name", 0)?;
let name = self.ident(name)?;
let name = if &*name == "_" { None } else { Some(name) };
let expr = self.expr_rhs(stmt, 1)?;
body.push(Stmt::Let(LetStmt { var: name, expr }));
}
"return" => {
trace!("return stmt");
let expr = self.expr_rhs(stmt, 0)?;
body.push(Stmt::Return(ReturnStmt { expr }));
}
"print" => {
trace!("print stmt");
let expr = self.expr_rhs(stmt, 0)?;
body.push(Stmt::Print(PrintStmt { expr }));
}
x => {
return Err(KdlScriptParseError {
message: format!("I don't know what a '{x}' statement is"),
src: self.src.clone(),
span: *stmt.name().span(),
help: None,
})?;
}
}
#[cfg(not(feature = "eval"))]
return Err(KdlScriptParseError {
message: format!("I don't know what a '{x}' statement is"),
src: self.src.clone(),
span: *stmt.name().span(),
help: None,
})?;
}
}
reached_body = true;
}
Ok(FuncDecl {
name,
inputs,
outputs,
#[cfg(feature = "eval")]
body,
attrs,
})
}
/// Parse an `@attribute` node.
fn attr(&mut self, attr: &KdlNode) -> Result<Attr> {
let entries = attr.entries();
let attr = match attr.name().value() {
"@packed" => {
trace!("packed attr");
self.no_children(attr)?;
Attr::Packed(AttrPacked {})
}
"@derive" => {
trace!("derive attr");
let traits = self.string_list(entries)?;
self.no_children(attr)?;
Attr::Derive(AttrDerive(traits))
}
"@" => {
trace!("passthrough attr");
let val = self.one_string(attr, "attribute to pass through to target language")?;
Attr::Passthrough(AttrPassthrough(val))
}
x => {
return Err(KdlScriptParseError {
message: format!("I don't know what a '{x}' attribute is"),
src: self.src.clone(),
span: *attr.name().span(),
help: None,
})?;
}
};
Ok(attr)
}
/// This node's entries should only be a positional list of strings.
fn string_list(&mut self, entries: &[KdlEntry]) -> Result<Vec<Spanned<String>>> {
entries
.iter()
.map(|e| -> Result<Spanned<String>> {
if e.name().is_some() {
return Err(KdlScriptParseError {
message: "Named values don't belong here, only strings".to_string(),
src: self.src.clone(),
span: *e.span(),
help: Some("try removing the name".to_owned()),
})?;
}
match e.value() {
kdl::KdlValue::RawString(s) | kdl::KdlValue::String(s) => {
Ok(Spanned::new(s.clone(), *e.span()))
}
_ => Err(KdlScriptParseError {
message: "This should be a string".to_string(),
src: self.src.clone(),
span: *e.span(),
help: Some("try adding quotes?".to_owned()),
})?,
}
})
.collect()
}
/// This node's entries should be only one string.
fn one_string(&mut self, node: &KdlNode, desc: &str) -> Result<Spanned<String>> {
let res = self.string_at(node, desc, 0)?;
let entries = node.entries();
if let Some(e) = entries.get(1) {
return Err(KdlScriptParseError {
message: format!("You have something extra after your {desc}"),
src: self.src.clone(),
span: *e.span(),
help: Some("remove this?".to_owned()),
})?;
}
Ok(res)
}
/// This node should have a string at this entry offset.
fn string_at(&mut self, node: &KdlNode, desc: &str, offset: usize) -> Result<Spanned<String>> {
let entries = node.entries();
if let Some(e) = entries.get(offset) {
if e.name().is_some() {
return Err(KdlScriptParseError {
message: "Named values don't belong here, only strings".to_string(),
src: self.src.clone(),
span: *e.span(),
help: Some("try removing the name".to_owned()),
})?;
}
match e.value() {
kdl::KdlValue::RawString(s) | kdl::KdlValue::String(s) => {
Ok(Spanned::new(s.clone(), *e.span()))
}
_ => Err(KdlScriptParseError {
message: format!("This should be a {desc} (string)"),
src: self.src.clone(),
span: *e.span(),
help: Some("try adding quotes".to_owned()),
})?,
}
} else {
let node_ident = node.name().span();
let after_ident = node_ident.offset() + node_ident.len();
Err(KdlScriptParseError {
message: format!("Hey I need a {desc} (string) here!"),
src: self.src.clone(),
span: (after_ident..after_ident).into(),
help: None,
})?
}
}
/// This node should have no entries.
fn no_args(&mut self, node: &KdlNode) -> Result<()> {
if let Some(entry) = node.entries().get(0) {
return Err(KdlScriptParseError {
message: "This shouldn't have arguments".to_string(),
src: self.src.clone(),
span: *entry.span(),
help: Some("delete them?".to_string()),
})?;
}
Ok(())
}
/// This node should have no children.
fn no_children(&mut self, node: &KdlNode) -> Result<()> {
if let Some(children) = node.children() {
return Err(KdlScriptParseError {
message: "These children should never have been born".to_string(),
src: self.src.clone(),
span: *children.span(),
help: Some("delete this block?".to_string()),
})?;
}
Ok(())
}
/// This node's children should be TypedVars
fn typed_var_children(&mut self, node: &KdlNode) -> Result<Vec<TypedVar>> {
node.children()
.into_iter()
.flat_map(|d| d.nodes())
.map(|var| {
let name = self.var_name_decl(var)?;
let ty_str = self.one_string(var, "type")?;
let ty = self.tydent(&ty_str)?;
self.no_children(var)?;
Ok(TypedVar { name, ty })
})
.collect()
}
/// This node's children should be enum variants
fn enum_variant_children(&mut self, node: &KdlNode) -> Result<Vec<EnumVariant>> {
node.children()
.into_iter()
.flat_map(|d| d.nodes())
.map(|var| {
let name = var.name();
let name = Spanned::new(name.value().to_owned(), *name.span());
let name = self.ident(name)?;
let entries = var.entries();
let val = if let Some(e) = entries.get(0) {
Some(self.int_expr(e)?)
} else {
None
};
if let Some(e) = entries.get(1) {
return Err(KdlScriptParseError {
message: "You have something extra after your enum case".to_string(),
src: self.src.clone(),
span: *e.span(),
help: Some("remove this?".to_owned()),
})?;
}
// TODO: deny any other members of `entries`
self.no_children(var)?;
Ok(EnumVariant { name, val })
})
.collect()
}
/// This node's children should be tagged variants
fn tagged_variant_children(&mut self, node: &KdlNode) -> Result<Vec<TaggedVariant>> {
node.children()
.into_iter()
.flat_map(|d| d.nodes())
.map(|var| {
self.no_args(var)?;
let name = var.name();
let name = Spanned::new(name.value().to_owned(), *name.span());
let name = self.ident(name)?;
let fields = if var.children().is_some() {
Some(self.typed_var_children(var)?)
} else {
None
};
Ok(TaggedVariant { name, fields })
})
.collect()
}
/// Parse this node's name as a possibly-positional variable name
///
/// TODO: probably want this `Option<Ident>` to be its own special enum?
fn var_name_decl(&mut self, var: &KdlNode) -> Result<Option<Ident>> {
let name = var.name();
let name = if name.value() == "_" {
None
} else {
let name = self.ident(Spanned::new(name.value().to_owned(), *name.span()))?;
Some(name)
};
Ok(name)
}
/// Parse a [`Tydent`][] from this String.
fn tydent(&mut self, input: &Spanned<String>) -> Result<Spanned<Tydent>> {
let (_, ty_ref) = all_consuming(context("a type", tydent))(input)
.finish()
.map_err(|_e| KdlScriptParseError {
message: String::from("couldn't parse type"),
src: self.src.clone(),
span: Spanned::span(input),
help: None,
})?;
Ok(ty_ref)
}
/// Parse an [`Ident`][] from this String.
fn ident(&mut self, input: Spanned<String>) -> Result<Spanned<String>> {
let (_, _) =
all_consuming(context("a type", tydent))(&input).map_err(|_e| KdlScriptParseError {
message: String::from("invalid identifier"),
src: self.src.clone(),
span: Spanned::span(&input),
help: None,
})?;
Ok(input)
}
/// Parse an [`IntExpr`][] (literal) from this entry.
fn int_expr(&mut self, entry: &KdlEntry) -> Result<IntExpr> {
if entry.name().is_some() {
return Err(KdlScriptParseError {
message: "Named values don't belong here, only literals".to_string(),
src: self.src.clone(),
span: *entry.span(),
help: Some("try removing the name".to_owned()),
})?;
}
let val = match entry.value() {
kdl::KdlValue::Base2(int)
| kdl::KdlValue::Base8(int)
| kdl::KdlValue::Base10(int)
| kdl::KdlValue::Base16(int) => *int,
_ => {
return Err(KdlScriptParseError {
message: String::from("must be an integer"),
src: self.src.clone(),
span: *entry.span(),
help: None,
})?;
}
};
Ok(IntExpr {
span: *entry.span(),
val,
})
}
}
// A fuckton of nom parsing for sub-syntax like Tydents.
type NomResult<I, O> = IResult<I, O, VerboseError<I>>;
/// Matches the syntax for tydent ("identifier, but for types") incl structural types like arrays/references.
fn tydent(input: &str) -> NomResult<&str, Spanned<Tydent>> {
alt((tydent_ref, tydent_array, tydent_empty_tuple, tydent_named))(input)
}
/// Matches a reference type (&T)
fn tydent_ref(input: &str) -> NomResult<&str, Spanned<Tydent>> {
let (input, pointee_ty) = preceded(
tag("&"),
context("pointee type", cut(preceded(many0(unicode_space), tydent))),
)(input)?;
// TODO: properly setup this span!
Ok((input, Spanned::from(Tydent::Ref(Box::new(pointee_ty)))))
}
/// Matches an array type ([T; N])
fn tydent_array(input: &str) -> NomResult<&str, Spanned<Tydent>> {
let (input, (elem_ty, array_len)) = delimited(
tag("["),
cut(separated_pair(
context(
"an element type",
delimited(many0(unicode_space), tydent, many0(unicode_space)),
),
tag(";"),
context(
"an array length (integer)",
delimited(many0(unicode_space), array_len, many0(unicode_space)),
),
)),
tag("]"),
)(input)?;
// TODO: properly setup these spans!
Ok((
input,
Spanned::from(Tydent::Array(Box::new(elem_ty), array_len)),
))
}
/// Matches an array length (u64)
fn array_len(input: &str) -> NomResult<&str, u64> {
nom::character::complete::u64(input)
}
/// Matches the empty tuple
fn tydent_empty_tuple(input: &str) -> NomResult<&str, Spanned<Tydent>> {
let (input, _tup) = tag("()")(input)?;
// TODO: properly setup this span!
Ok((input, Spanned::from(Tydent::Empty)))
}
/// Matches a named type
fn tydent_named(input: &str) -> NomResult<&str, Spanned<Tydent>> {
let (input, (ty_name, generics)) = pair(
ident,
opt(delimited(
pair(unicode_space, tag("<")),
cut(separated_list1(
tag(","),
delimited(unicode_space, tydent_ref, unicode_space),
)),
tag(">"),
)),
)(input)?;
if let Some(_generics) = generics {
panic!("generics aren't yet implemented!");
}
// TODO: properly setup this span!
Ok((
input,
Spanned::from(Tydent::Name(Spanned::from(ty_name.to_owned()))),
))
}
/// Matches an identifier
fn ident(input: &str) -> NomResult<&str, &str> {
recognize(pair(
alt((alpha1, tag("_"))),
many0_count(alt((alphanumeric1, tag("_")))),
))(input)
}
/// Matches various kinds of whitespace we allow
fn unicode_space(input: &str) -> NomResult<&str, &str> {
alt((
tag(" "),
tag("\t"),
tag("\u{FEFF}"), // BOM
tag("\u{00A0}"),
tag("\u{1680}"),
tag("\u{2000}"),
tag("\u{2001}"),
tag("\u{2002}"),
tag("\u{2003}"),
tag("\u{2004}"),
tag("\u{2005}"),
tag("\u{2006}"),
tag("\u{2007}"),
tag("\u{2008}"),
tag("\u{2009}"),
tag("\u{200A}"),
tag("\u{202F}"),
tag("\u{205F}"),
tag("\u{3000}"),
))(input)
}
/// An integer expression (literal)
///
/// TODO: this should actually defer deserializing into an integer
/// so that it can be some huge type like `u256`. Not sure who/where
/// would be responsible for validating that the value fits in the
/// expected range for where it's placed!
///
/// Possibly the type checker, but it also kinda needs to be deferred
/// to the target language backends as a language may not be able to
/// handle a `u256` or whatever.
#[derive(Debug, Clone)]
pub struct IntExpr {
pub span: SourceSpan,
pub val: i64,
}
/// All of this gunk is only used for function bodies, which only
/// exist in the meme `feature=eval` mode.
#[cfg(feature = "eval")]
pub use runnable::*;
#[cfg(feature = "eval")]
mod runnable {
use super::*;
#[derive(Debug, Clone)]
pub enum Stmt {
Let(LetStmt),
Return(ReturnStmt),
Print(PrintStmt),
}
#[derive(Debug, Clone)]
pub struct LetStmt {
pub var: Option<Ident>,
pub expr: Spanned<Expr>,
}
#[derive(Debug, Clone)]
pub struct ReturnStmt {
pub expr: Spanned<Expr>,
}
#[derive(Debug, Clone)]
pub struct PrintStmt {
pub expr: Spanned<Expr>,
}
#[derive(Debug, Clone)]
pub enum Expr {
Call(CallExpr),
Path(PathExpr),
Ctor(CtorExpr),
Literal(LiteralExpr),
}
#[derive(Debug, Clone)]
pub struct CallExpr {
pub func: Ident,
pub args: Vec<Spanned<Expr>>,
}
#[derive(Debug, Clone)]
pub struct PathExpr {
pub var: Ident,
pub path: Vec<Ident>,
}
#[derive(Debug, Clone)]
pub struct CtorExpr {
pub ty: Ident,
pub vals: Vec<Spanned<LetStmt>>,
}
#[derive(Debug, Clone)]
pub struct LiteralExpr {
pub span: SourceSpan,
pub val: Literal,
}
#[derive(Debug, Clone)]
pub enum Literal {
Float(f64),
Int(i64),
Bool(bool),
}
impl Parser<'_> {
pub(crate) fn func_args(
&mut self,
node: &KdlNode,
expr_start: usize,
) -> Result<Vec<Spanned<Expr>>> {
node.entries()[expr_start..]
.iter()
.enumerate()
.map(|(idx, _e)| self.smol_expr(node, expr_start + idx))
.collect()
}
pub(crate) fn literal_expr(&mut self, entry: &KdlEntry) -> Result<LiteralExpr> {
if entry.name().is_some() {
return Err(KdlScriptParseError {
message: "Named values don't belong here, only literals".to_string(),
src: self.src.clone(),
span: *entry.span(),
help: Some("try removing the name".to_owned()),
})?;
}
let val = match entry.value() {
kdl::KdlValue::RawString(_) | kdl::KdlValue::String(_) => {
return Err(KdlScriptParseError {
message: "strings aren't supported literals".to_string(),
src: self.src.clone(),
span: *entry.span(),
help: None,
})?;
}
kdl::KdlValue::Null => {
return Err(KdlScriptParseError {
message: "nulls aren't supported literals".to_string(),
src: self.src.clone(),
span: *entry.span(),
help: None,
})?;
}
kdl::KdlValue::Base2(int)
| kdl::KdlValue::Base8(int)
| kdl::KdlValue::Base10(int)
| kdl::KdlValue::Base16(int) => Literal::Int(*int),
kdl::KdlValue::Base10Float(val) => Literal::Float(*val),
kdl::KdlValue::Bool(val) => Literal::Bool(*val),
};
Ok(LiteralExpr {
span: *entry.span(),
val,
})
}
pub(crate) fn expr_rhs(
&mut self,
node: &KdlNode,
expr_start: usize,
) -> Result<Spanned<Expr>> {
trace!("expr rhs");
let expr = if let Ok(string) = self.string_at(node, "", expr_start) {
if let Some((func, "")) = string.rsplit_once(':') {
trace!(" call expr");
let func = Spanned::new(func.to_owned(), Spanned::span(&string));
let args = self.func_args(node, expr_start + 1)?;
Expr::Call(CallExpr { func, args })
} else if node.children().is_some() {
trace!(" ctor expr");
let ty = string;
let vals = self.let_stmt_children(node)?;
Expr::Ctor(CtorExpr { ty, vals })
} else {
trace!(" path expr");
let mut parts = string.split('.');
let var =
Spanned::new(parts.next().unwrap().to_owned(), Spanned::span(&string));
let path = parts
.map(|s| Spanned::new(s.to_owned(), Spanned::span(&string)))
.collect();
Expr::Path(PathExpr { var, path })
}
} else if let Some(val) = node.entries().get(expr_start) {
trace!(" literal expr");
Expr::Literal(self.literal_expr(val)?)
} else {
return Err(KdlScriptParseError {
message: "I thought there was supposed to be an expression after here?"
.to_string(),
src: self.src.clone(),
span: *node.span(),
help: None,
})?;
};
Ok(Spanned::new(expr, *node.span()))
}
pub(crate) fn smol_expr(
&mut self,
node: &KdlNode,
expr_at: usize,
) -> Result<Spanned<Expr>> {
trace!("smol expr");
let expr = if let Ok(string) = self.string_at(node, "", expr_at) {
if let Some((_func, "")) = string.rsplit_once(':') {
return Err(KdlScriptParseError {
message:
"Nested function calls aren't supported because this is a shitpost"
.to_string(),
src: self.src.clone(),
span: *node.span(),
help: None,
})?;
} else if node.children().is_some() {
return Err(KdlScriptParseError {
message: "Ctors exprs can't be nested in function calls because this is a shitpost".to_string(),
src: self.src.clone(),
span: *node.span(),
help: None,
})?;
} else {
trace!(" path expr");
let mut parts = string.split('.');
let var =
Spanned::new(parts.next().unwrap().to_owned(), Spanned::span(&string));
let path = parts
.map(|s| Spanned::new(s.to_owned(), Spanned::span(&string)))
.collect();
Expr::Path(PathExpr { var, path })
}
} else if let Some(val) = node.entries().get(expr_at) {
trace!(" literal expr");
Expr::Literal(self.literal_expr(val)?)
} else {
return Err(KdlScriptParseError {
message: "I thought there was supposed to be an expression after here?"
.to_string(),
src: self.src.clone(),
span: *node.span(),
help: None,
})?;
};
Ok(Spanned::new(expr, *node.span()))
}
pub(crate) fn let_stmt_children(
&mut self,
node: &KdlNode,
) -> Result<Vec<Spanned<LetStmt>>> {
node.children()
.into_iter()
.flat_map(|d| d.nodes())
.map(|var| {
let name = self.var_name_decl(var)?;
let expr = self.expr_rhs(var, 0)?;
Ok(Spanned::new(LetStmt { var: name, expr }, *var.span()))
})
.collect()
}
}
impl ParsedProgram {
pub(crate) fn add_builtin_funcs(&mut self) -> Result<()> {
// Add builtins jankily
self.funcs.insert(
Spanned::from(String::from("+")),
FuncDecl {
name: Spanned::from(String::from("+")),
inputs: vec![
TypedVar {
name: Some(Spanned::from(String::from("lhs"))),
ty: Spanned::from(Tydent::Name(Spanned::from(String::from("i64")))),
},
TypedVar {
name: Some(Spanned::from(String::from("rhs"))),
ty: Spanned::from(Tydent::Name(Spanned::from(String::from("i64")))),
},
],
outputs: vec![TypedVar {
name: Some(Spanned::from(String::from("out"))),
ty: Spanned::from(Tydent::Name(Spanned::from(String::from("i64")))),
}],
attrs: vec![],
body: vec![],
},
);
Ok(())
}
}
}