Skip to content

Commit

Permalink
nixd/Syntax: basic parsing, with the test
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Sep 17, 2023
1 parent ef82b5d commit 6a7dbaa
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 194 deletions.
15 changes: 15 additions & 0 deletions nixd/include/nixd/Syntax/Nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,25 @@ namespace nixd::syntax {
/// TODO: the comment
struct Node {
nixd::RangeIdx Range;
enum NodeKind {
NK_Node,
#define NODE(NAME, BODY) NK_##NAME,
#include "Nodes.inc"
#undef NODE
};
virtual NodeKind getKind() { return NK_Node; };
virtual ~Node() = default;
};

#define COMMON_METHOD virtual NodeKind getKind() override;
#define NODE(NAME, BODY) struct NAME : Node BODY;
#include "Nodes.inc"
#undef NODE
#undef COMMON_METHOD

#define NODE(NAME, _) \
inline Node::NodeKind NAME::getKind() { return NK_##NAME; }
#include "Nodes.inc"
#undef NODE

} // namespace nixd::syntax
137 changes: 114 additions & 23 deletions nixd/include/nixd/Syntax/Nodes.inc
Original file line number Diff line number Diff line change
@@ -1,69 +1,139 @@
#ifdef NODE
NODE(Identifier, { nix::Symbol Symbol; })

#ifndef COMMON_METHOD
#define COMMON_METHOD
#define P_COMMON_METHOD_DEFINED
#endif

NODE(Identifier, {
nix::Symbol Symbol;
COMMON_METHOD
})
NODE(Formal, {
Identifier *ID;
/// The default argument.
Node *Default;
COMMON_METHOD
})
NODE(Formals, {
std::vector<Formal *> Formals;
bool Ellipsis;
COMMON_METHOD
})
NODE(Function, {
Identifier *Arg;
Formals *Formals;
Node *Body;
COMMON_METHOD
})
NODE(Assert, {
Node *Cond;
Node *Body;
COMMON_METHOD
})
NODE(With, {
Node *Attrs;
Node *Body;
COMMON_METHOD
})
NODE(Binds, {
std::vector<Node *> Attributes;
COMMON_METHOD
})
NODE(Binds, { std::vector<Node *> Attributes; })

NODE(ListBody, { std::vector<Node *> Elems; })
NODE(List, { ListBody *Body; })
NODE(ListBody, {
std::vector<Node *> Elems;
COMMON_METHOD
})
NODE(List, {
ListBody *Body;
COMMON_METHOD
})

NODE(Let, {
Node *Binds;
Node *Body;
COMMON_METHOD
})
NODE(If, {
Node *Cond;
Node *Then;
Node *Else;
COMMON_METHOD
})
NODE(Variable, {
Node *ID;
COMMON_METHOD
})
NODE(Int, {
nix::NixInt N;
COMMON_METHOD
})
NODE(Float, {
nix::NixFloat NF;
COMMON_METHOD
})
NODE(InterpExpr, {
Node *Body;
COMMON_METHOD
})
NODE(String, {
std::string S;
COMMON_METHOD
})
NODE(IndString, {
std::string S;
COMMON_METHOD
})
NODE(IndStringParts, {
std::vector<Node *> SubStrings;
COMMON_METHOD
})
NODE(ConcatStrings, {
std::vector<Node *> SubStrings;
COMMON_METHOD
})
NODE(HPath, {
std::string S;
COMMON_METHOD
})
NODE(Path, {
std::string S;
COMMON_METHOD
})
NODE(SPath, {
std::string S;
COMMON_METHOD
})
NODE(URI, {
std::string S;
COMMON_METHOD
})
NODE(Variable, { Node *ID; })
NODE(Int, { nix::NixInt N; })
NODE(Float, { nix::NixFloat NF; })
NODE(InterpExpr, { Node *Body; })
NODE(String, { std::string S; })
NODE(IndString, { std::string S; })
NODE(IndStringParts, { std::vector<Node *> SubStrings; })
NODE(ConcatStrings, { std::vector<Node *> SubStrings; })
NODE(HPath, { std::string S; })
NODE(Path, { std::string S; })
NODE(SPath, { std::string S; })
NODE(URI, { std::string S; })

NODE(Call, {
Node *Fn;
std::vector<Node *> Args;
COMMON_METHOD
})
NODE(AttrPath, {
std::vector<Node *> Names;
COMMON_METHOD
})
NODE(Attrs, {
std::vector<Node *> Names;
COMMON_METHOD
})
NODE(AttrPath, { std::vector<Node *> Names; })
NODE(Attrs, { std::vector<Node *> Names; })

NODE(Attribute, {
AttrPath *Path;
Node *Body;
COMMON_METHOD
})

NODE(InheritedAttribute, {
Attrs *Attrs;
Node *E;
COMMON_METHOD
})

NODE(Select, {
Expand All @@ -74,31 +144,52 @@ NODE(Select, {
/// expr1 '.' attrpath 'or' expr2
/// expr2 is the default value if we cannot select
Node *Default;
COMMON_METHOD
})

NODE(OpHasAttr, {
Node *Operand;
Node *Path;
COMMON_METHOD
})
NODE(OpNot, {
Node *Body;
COMMON_METHOD
})
NODE(OpNegate, {
Node *Body;
COMMON_METHOD
})
NODE(OpNot, { Node *Body; })
NODE(OpNegate, { Node *Body; })

NODE(Braced, { Node *Body; })
NODE(Braced, {
Node *Body;
COMMON_METHOD
})

NODE(AttrSet, {
Binds *Binds;
bool Recursive;
COMMON_METHOD
})

NODE(LegacyLet, { Binds *Binds; })
NODE(LegacyLet, {
Binds *Binds;
COMMON_METHOD
})

// Binary Operators
#define BIN_OP(NAME, _) \
NODE(NAME, { \
Node *LHS; \
Node *RHS; \
COMMON_METHOD \
})
#include "BinaryOps.inc"
#undef BIN_OP

#endif
#ifdef P_COMMON_METHOD_DEFINED
#undef COMMON_METHOD
#undef P_COMMON_METHOD_DEFINED
#endif // P_COMMON_METHOD_DEFINED

#endif // NODE
7 changes: 7 additions & 0 deletions nixd/include/nixd/Syntax/Parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "Parser/Require.h"

namespace nixd::syntax {
void parse(char *Text, size_t Size, ParseData *Data);
} // namespace nixd::syntax
2 changes: 2 additions & 0 deletions nixd/include/nixd/Syntax/Parser/Require.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ struct ParseState {
};

struct ParseData {
Node *Result;

ParseState State;

nix::PosTable::Origin Origin;
Expand Down
2 changes: 1 addition & 1 deletion nixd/lib/AST/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
libnixdASTDeps = [ nixd_lsp_server
, nix_all
, nixdExpr
, nixdParser
, nixdSyntax
, nixdNix
, llvm
]
Expand Down
20 changes: 20 additions & 0 deletions nixd/lib/Syntax/Parser/Epilogue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "Parser.tab.h"

#include "Lexer.tab.h"

#include "nixd/Syntax/Parser.h"
#include "nixd/Syntax/Parser/Require.h"

namespace nixd::syntax {

void parse(char *Text, size_t Size, ParseData *Data) {
yyscan_t Scanner;
yylex_init(&Scanner);
yy_scan_buffer(Text, Size, Scanner);
yyparse(Scanner, Data);
yylex_destroy(Scanner);
}

} // namespace nixd::syntax
Loading

0 comments on commit 6a7dbaa

Please sign in to comment.