Add syntax for package and library (#792)

Doesn't add much logic, only takes advantage of parser structure for the ordering enforcement.

Note import_nonexistent tests should probably fail, but writing import tests needs a chain of functionality, and I figured I'd just start adding some to validate the syntax (not adding existent imports because that'd require multi-file structure).
This commit is contained in:
Jon Meow
2021-09-02 16:01:15 -07:00
committed by GitHub
parent 0980df65cd
commit a03536a196
152 changed files with 433 additions and 63 deletions
+47 -3
View File
@@ -65,7 +65,7 @@
%code requires {
#include <optional>
#include "executable_semantics/ast/abstract_syntax_tree.h"
#include "executable_semantics/ast/ast.h"
#include "executable_semantics/ast/declaration.h"
#include "executable_semantics/ast/expression.h"
#include "executable_semantics/ast/function_definition.h"
@@ -93,6 +93,11 @@
%token <std::string> sized_type_literal
%token <std::string> string_literal
%type <std::string> designator
%type <std::pair<LibraryName, bool>> package_directive
%type <LibraryName> import_directive
%type <std::vector<LibraryName>> import_directives
%type <std::string> optional_library_path
%type <bool> api_or_impl
%type <BisonWrap<Ptr<const Declaration>>> declaration
%type <BisonWrap<Ptr<const FunctionDefinition>>> function_declaration
%type <BisonWrap<Ptr<const FunctionDefinition>>> function_definition
@@ -133,6 +138,7 @@
%token
// Most tokens have their spelling defined in lexer.lpp.
AND
API
ARROW
AUTO
AWAIT
@@ -156,13 +162,17 @@
FN
FNTY
IF
IMPL
IMPORT
LEFT_CURLY_BRACE
LEFT_PARENTHESIS
LEFT_SQUARE_BRACKET
LIBRARY
MATCH
MINUS
NOT
OR
PACKAGE
PERIOD
PLUS
RETURN
@@ -217,8 +227,42 @@
%start input
%%
input: declaration_list
{ parsed_program = $1; }
input: package_directive import_directives declaration_list
{
parsed_program = AST({.package = $1.first,
.is_api = $1.second,
.imports = std::move($2),
.declarations = std::move($3)});
}
;
package_directive:
PACKAGE identifier optional_library_path api_or_impl SEMICOLON
{ $$ = {LibraryName({.package = $2, .path = $3}), $4}; }
;
import_directive:
IMPORT identifier optional_library_path SEMICOLON
{ $$ = LibraryName({.package = $2, .path = $3}); }
;
import_directives:
// Empty
{ $$ = std::vector<LibraryName>(); }
| import_directives import_directive
{
$$ = std::move($1);
$$.push_back($2);
}
;
optional_library_path:
// Empty
{ $$ = ""; }
| LIBRARY string_literal
{ $$ = $2; }
;
api_or_impl:
API
{ $$ = true; }
| IMPL
{ $$ = false; }
;
expression:
identifier