-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add parser for pkg-config files #94
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
_flex_version = '>= 2.6' | ||
lunacd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_bison_version = '>= 2.6' | ||
prog_flex = find_program('', required : false) | ||
flex_args = [] | ||
prog_bison = find_program('', required : false) | ||
if host_machine.system() == 'windows' | ||
prog_flex = find_program('win_flex', required : false, version : _flex_vesrion) | ||
if prog_flex.found() | ||
# This uses <io.h> instead of <unistd.h> | ||
flex_args = ['--wincompat'] | ||
endif | ||
|
||
prog_bison = find_program('win_bison', required : false, version : _bison_version) | ||
endif | ||
if not prog_flex.found() | ||
prog_flex = find_program('flex', version : _flex_version) | ||
endif | ||
if not prog_bison.found() | ||
prog_bison = find_program('bison', version : _bison_version) | ||
endif | ||
|
||
pc_scanner = custom_target( | ||
'pc_scanner', | ||
command : [prog_flex, flex_args, '--outfile=@OUTPUT0@', '--header-file=@OUTPUT1@', '@INPUT@'], | ||
input : 'pc.l', | ||
output : ['@[email protected]', '@[email protected]'], | ||
) | ||
|
||
pc_parser = custom_target( | ||
'pc_parser', | ||
command : [prog_bison, '-d', '@INPUT@', '-v', '--output=@OUTPUT0@', '--defines=@OUTPUT1@'], | ||
input : 'pc.y', | ||
output : ['@[email protected]', '@[email protected]', 'locations.hpp'] | ||
) | ||
lunacd marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* Copyright © 2024 Haowen Liu | ||
*/ | ||
|
||
%option noyywrap | ||
|
||
%{ | ||
#include <string> | ||
#include <istream> | ||
#include "cps/pc_compat/pc_loader.hpp" | ||
#include "cps/pc_compat/pc.parser.hpp" | ||
%} | ||
|
||
/* To debug scanner, set debug as an option */ | ||
/* TODO: Add a way to debug scanner without rebuilding */ | ||
%option noyywrap nounput noinput batch | ||
|
||
%{ | ||
std::istream *yyinput = nullptr; | ||
|
||
#define YY_INPUT(buf,result,max_size) { \ | ||
yyinput->read(buf, max_size); \ | ||
result = yyinput->gcount(); \ | ||
} | ||
%} | ||
|
||
str [^ \t\r\n#:=${}]+ | ||
blank [ \t\r]+ | ||
comment #[^\n]* | ||
|
||
%% | ||
":" { return yy::parser::make_COLON(); } | ||
"=" { return yy::parser::make_EQ(); } | ||
"<" { return yy::parser::make_LT(); } | ||
"<=" { return yy::parser::make_LE(); } | ||
"!=" { return yy::parser::make_NE(); } | ||
">=" { return yy::parser::make_GE(); } | ||
">" { return yy::parser::make_GT(); } | ||
"\n" { return yy::parser::make_LF(); } | ||
"$" { return yy::parser::make_DOLLAR(); } | ||
"{" { return yy::parser::make_LBRACE(); } | ||
"}" { return yy::parser::make_RBRACE(); } | ||
"," { return yy::parser::make_COMMA(); } | ||
"Requires" { return yy::parser::make_REQUIRES(); } | ||
"Requires.private" { return yy::parser::make_REQUIRES_P(); } | ||
"Conflicts" { return yy::parser::make_CONFLICTS(); } | ||
"Provides" { return yy::parser::make_PROVIDES(); } | ||
|
||
{comment} {} | ||
{blank} { return yy::parser::make_BLANK(yytext); } | ||
{str} { return yy::parser::make_STR(yytext); } | ||
<<EOF>> { return yy::parser::make_YYEOF(); } | ||
%% | ||
|
||
namespace cps::pc_compat { | ||
|
||
void PcLoader::scan_begin(std::istream &istream) const { | ||
yyinput = &istream; | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// SPDX-License-Identifier: MIT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an existing BNF grammar for pkg config files we can refer to or quote? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not that I know of. But there probably is? I was using https://manpages.ubuntu.com/manpages/jammy/man5/pc.5.html as my reference There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see one either, and pkg-config uses a hand-rolled recursive descent parser. One thing I note from that is CFlags can also be spelled Cflags. 🤷 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pkgconf also uses a hand rolled recursive descent parser, but that man page is from pkgconf. My preference would be to emulate pkgconf rather than pkg-config if possible, since it actually has developers, and most distros are shipping pkgconf with a symlink to pkg-config There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I'm fine with landing in the current form, and if we encounter compatibility issues then we can make tweaks as needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a branch of pkgconf (that probably needs some additional work) that ports the kyua tests to a python runner. I can try to get those around for cps-config There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. When you do, feel free to ping me to fix any issues (I bet there would be a lot) |
||
// Copyright © 2024 Haowen Liu | ||
|
||
%skeleton "lalr1.cc" | ||
|
||
%define api.token.raw | ||
|
||
%define api.token.constructor | ||
%define api.value.type variant | ||
%define parse.assert | ||
|
||
%code requires { | ||
#include "cps/utils.hpp" | ||
#include "cps/pc_compat/pc_base.hpp" | ||
} | ||
|
||
// The parsing context. | ||
%param { cps::pc_compat::PcLoader& loader } | ||
|
||
%define parse.trace | ||
%define parse.error detailed | ||
%define parse.lac full | ||
|
||
%code { | ||
#include "cps/pc_compat/pc_loader.hpp" | ||
} | ||
|
||
%define api.token.prefix {TOK_} | ||
%token | ||
COLON ":" | ||
LF "\n" | ||
EQ "=" | ||
LT "<" | ||
LE "<=" | ||
NE "!=" | ||
GT ">" | ||
GE ">=" | ||
DOLLAR "$" | ||
LBRACE "{" | ||
RBRACE "}" | ||
COMMA "," | ||
REQUIRES "Requires" | ||
REQUIRES_P "Requires.private" | ||
CONFLICTS "Conflicts" | ||
PROVIDES "Provides" | ||
; | ||
|
||
%token <std::string> STR "str" | ||
%token <std::string> BLANK "blank" | ||
%nterm <std::string> literal | ||
%nterm <std::string> variable | ||
%nterm <std::string> name | ||
%nterm <cps::pc_compat::VersionOperation> version_op_token | ||
%nterm <cps::pc_compat::VersionOperation> version_op | ||
%nterm <cps::pc_compat::PackageRequirement> package_requirement | ||
%nterm <std::vector<cps::pc_compat::PackageRequirement>> package_requirements | ||
%nterm <std::variant<std::string, std::vector<cps::pc_compat::PackageRequirement>>> literal_property | ||
|
||
%printer { yyo << $$; } <*>; | ||
|
||
%% | ||
%start file; | ||
|
||
// This rule allows for the last line to not be terminated by '\n' | ||
file: | ||
lines | ||
| lines statement; | ||
|
||
lines: | ||
%empty | ||
| lines line; | ||
|
||
// In this grammar, leading whitespace is handled by this rule. | ||
// Everything else only takes care of trailing whitespace. | ||
line: | ||
"\n" | ||
| statement "\n" | ||
| "blank" statement "\n"; | ||
|
||
// Statement is a meaningful line, not including line feed | ||
statement: | ||
property | ||
| assignment; | ||
|
||
// property is a line that sets a property to a value | ||
property: | ||
"Requires" colon package_requirements { loader.properties.emplace("Requires", $3); } | ||
| "Requires.private" colon package_requirements { loader.properties.emplace("Requires.private", $3); } | ||
| "Conflicts" colon package_requirements { loader.properties.emplace("Conflicts", $3); } | ||
| "Provides" colon package_requirements { loader.properties.emplace("Provides", $3); } | ||
| name colon literal_property { loader.properties.emplace($1, $3); } | ||
|
||
// version_op_token captures all the valid tokens for version comparison | ||
version_op_token: | ||
"<" { $$ = cps::pc_compat::VersionOperation::lt; } | ||
| "<=" { $$ = cps::pc_compat::VersionOperation::le; } | ||
| "=" { $$ = cps::pc_compat::VersionOperation::eq; } | ||
| "!=" { $$ = cps::pc_compat::VersionOperation::ne; } | ||
| ">" { $$ = cps::pc_compat::VersionOperation::gt; } | ||
| ">=" { $$ = cps::pc_compat::VersionOperation::ge; }; | ||
|
||
// version_op handles trailing space for version comparisons | ||
version_op: | ||
version_op_token | ||
| version_op_token "blank" { $$ = $1; }; | ||
|
||
// package_requirement parses a package name, optionally followed by some version requirement | ||
package_requirement: | ||
name version_op "str" { | ||
$$ = cps::pc_compat::PackageRequirement { | ||
.package = $1, | ||
.operation = $2, | ||
.version = $3, | ||
}; | ||
} | ||
| package_requirement "blank" { $$ = $1; }; | ||
|
||
// package_requirements is a comma separated list of package_requirement | ||
package_requirements: | ||
package_requirement { $$ = std::vector{$1}; } | ||
| package_requirements comma package_requirement { | ||
$1.emplace_back($3); | ||
$$ = $1; | ||
} | ||
| package_requirements comma "str" { | ||
$1.emplace_back(cps::pc_compat::PackageRequirement { | ||
.package = $3, | ||
.operation = std::nullopt, | ||
.version = std::nullopt, | ||
}); | ||
$$ = $1; | ||
}; | ||
|
||
// assignment is a line that sets a variable to a value | ||
assignment: | ||
name "=" literal { loader.variables.emplace($1, cps::utils::trim($3)); }; | ||
|
||
// name handles surrounding spaces for a variable or property name | ||
name: | ||
"str" | ||
| "str" "blank" { $$ = $1; }; | ||
|
||
// literal_property constructs a variant with the trimmed literal value | ||
literal_property: | ||
literal { $$ = cps::pc_compat::PcPropertyValue{std::in_place_type<std::string>, cps::utils::trim($1)}; }; | ||
|
||
// Literal is a literal string. This could contain trailing whitespace so trim the result before using. | ||
literal: | ||
":" { $$ = ":"; } | ||
| "str" { $$ = $1; } | ||
| variable { $$ = $1; } | ||
| literal ":" { $$ = $1 + ":"; } | ||
| literal "str" { $$ = $1 + $2; } | ||
| literal variable { $$ = $1 + $2; } | ||
| literal "blank" { $$ = $1 + $2; }; | ||
|
||
variable: | ||
"$" "{" "str" "}" { $$ = loader.variables[$3]; } | ||
|
||
// colon and comma handles trailing whitespace | ||
colon: | ||
":" | ||
| ":" "blank"; | ||
comma: | ||
"," | ||
| "," "blank"; | ||
%% | ||
|
||
void | ||
yy::parser::error (const std::string& m) | ||
{ | ||
std::cerr << "Error: " << m << '\n'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Copyright © 2024 Haowen Liu | ||
|
||
// This header is created to avoid writing non-trivial C++ code in pc.y. | ||
// This header contains types and declarations that are needed by both loader and parser. | ||
|
||
#pragma once | ||
|
||
#include <optional> | ||
#include <ostream> | ||
#include <string> | ||
#include <variant> | ||
#include <vector> | ||
|
||
namespace cps::pc_compat { | ||
class PcLoader; | ||
|
||
// The following types needs to be declared in the parser because | ||
// PackageRequirement is the type of a non-terminal. bison needs | ||
// to do a sizeof on this type and cannot do so with a forward | ||
// declaration. | ||
enum class VersionOperation { lt, le, ne, eq, gt, ge }; | ||
struct PackageRequirement { | ||
std::string package; | ||
std::optional<VersionOperation> operation; | ||
std::optional<std::string> version; | ||
|
||
// For parser debug output | ||
friend std::ostream & operator<<(std::ostream & ost, const PackageRequirement & package_requirement); | ||
}; | ||
|
||
// For parser debug output | ||
std::ostream & operator<<(std::ostream & ost, const std::optional<VersionOperation> & version_operation); | ||
std::ostream & operator<<(std::ostream & ost, const std::vector<PackageRequirement> & package_requirements); | ||
std::ostream & operator<<(std::ostream & ost, | ||
const std::variant<std::string, std::vector<PackageRequirement>> & property_value); | ||
} // namespace cps::pc_compat |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigh
It seems probable that this is how the CMake modules for Bison and Flex were designed, but for the record, it's gross to expand strings into add_library calls instead of having a
target_bison_codegen
function that just adds the source files to your target for you. The function should also be adding the needed include directory for you so you don't have to add "magic" include paths as you do below.Let's circle back with Kitware about whether the flex and bison codegen need another look, especially now that CMake 3.31 will support code generation better than before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to close the feedback loop, this is still something that needs cleaned up if someone's looking for a good upstream CMake contribution to start with. But it wasn't scoped into any work yet. Using the API like this is the best thing we can do for now.