Skip to content

Commit 292bcd1

Browse files
committed
Implement ability to compile directly from stdin
1 parent dc3758a commit 292bcd1

File tree

4 files changed

+85
-28
lines changed

4 files changed

+85
-28
lines changed

source/common.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,9 @@ class cmdline_processor
799799
[](auto& a, auto& b){ return a.group < b.group || (a.group == b.group && a.name < b.name); }
800800
);
801801

802-
print("\nUsage: cppfront [options] file ...\n\nOptions:\n");
802+
print("\nUsage: cppfront [options] file ...\n");
803+
print("\nfile - The source file(s) to compile (can be 'stdin' to read text directly) \n");
804+
print("\nOptions: \n");
803805
int last_group = -1;
804806
for (auto& flag : flags) {
805807
// Skip hidden flags

source/cppfront.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ auto main(
8484
}
8585

8686
// Load + lex + parse + sema
87-
cppfront c(arg.text);
87+
cppfront c = [&]() -> cppfront {
88+
if (arg.text == "stdin")
89+
return cppfront(std::cin);
90+
else
91+
return cppfront(arg.text);
92+
}();
8893

8994
// Generate Cpp1 (this may catch additional late errors)
9095
auto count = c.lower_to_cpp1();

source/io.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,20 @@ class source
887887
return false;
888888
}
889889

890+
return load(in);
891+
}
892+
893+
//-----------------------------------------------------------------------
894+
// load: Read a line-by-line view of a source file, preserving line breaks
895+
//
896+
// in the loaded source file
897+
// source program textual representation
898+
//
899+
auto load(
900+
std::istream& in
901+
)
902+
-> bool
903+
{
890904
auto in_comment = false;
891905
auto in_string_literal = false;
892906
auto in_raw_string_literal = false;

source/to_cpp1.h

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,37 +1202,73 @@ class cppfront
12021202

12031203
else
12041204
{
1205-
// Tokenize
1206-
//
1207-
tokens.lex(source.get_lines());
1208-
1209-
// Parse
1210-
//
1211-
try
1212-
{
1213-
for (auto const& [line, entry] : tokens.get_map()) {
1214-
if (!parser.parse(entry, tokens.get_generated())) {
1215-
errors.emplace_back(
1216-
source_position(line, 0),
1217-
"parse failed for section starting here",
1218-
false,
1219-
true // a noisy fallback error message
1220-
);
1221-
}
1222-
}
1205+
process_cpp2();
1206+
}
1207+
}
12231208

1224-
// Sema
1225-
parser.visit(sema);
1226-
if (!sema.apply_local_rules()) {
1227-
violates_initialization_safety = true;
1228-
}
1229-
}
1230-
catch (std::runtime_error& e) {
1209+
//-----------------------------------------------------------------------
1210+
// Constructor
1211+
//
1212+
// source_stream the contents of a source file to be processed
1213+
//
1214+
cppfront(std::istream& source_stream)
1215+
: sourcefile{ "stdin.cpp2" }
1216+
, source { errors }
1217+
, tokens { errors }
1218+
, parser { errors, includes }
1219+
, sema { errors }
1220+
{
1221+
// Load the program file into memory
1222+
//
1223+
if (!source.load(source_stream))
1224+
{
1225+
if (errors.empty()) {
12311226
errors.emplace_back(
12321227
source_position(-1, -1),
1233-
e.what()
1228+
"error reading source content from stdin"
12341229
);
12351230
}
1231+
source_loaded = false;
1232+
}
1233+
1234+
else
1235+
{
1236+
process_cpp2();
1237+
}
1238+
}
1239+
1240+
auto process_cpp2() -> void
1241+
{
1242+
// Tokenize
1243+
//
1244+
tokens.lex(source.get_lines());
1245+
1246+
// Parse
1247+
//
1248+
try
1249+
{
1250+
for (auto const& [line, entry] : tokens.get_map()) {
1251+
if (!parser.parse(entry, tokens.get_generated())) {
1252+
errors.emplace_back(
1253+
source_position(line, 0),
1254+
"parse failed for section starting here",
1255+
false,
1256+
true // a noisy fallback error message
1257+
);
1258+
}
1259+
}
1260+
1261+
// Sema
1262+
parser.visit(sema);
1263+
if (!sema.apply_local_rules()) {
1264+
violates_initialization_safety = true;
1265+
}
1266+
}
1267+
catch (std::runtime_error& e) {
1268+
errors.emplace_back(
1269+
source_position(-1, -1),
1270+
e.what()
1271+
);
12361272
}
12371273
}
12381274

0 commit comments

Comments
 (0)