LILAC
Language to language Interop LAyer Compiler
Loading...
Searching...
No Matches
module.cxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024 Yeong-won Seo
3 *
4 * This file is part of LILAC.
5 *
6 * LILAC is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3, or (at your option) any later
9 * version.
10 *
11 * LILAC is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <iostream>
21
22#include "pch.h"
23#include "shared/backend.h"
24#include "module.h"
25
26#include "interfacevisitor.h"
27#include "shared/exception.h"
28
29std::map<int, std::string> errorMap = {
30 { frxml::S_OK, "Operation completed successfully" },
31 { frxml::E_NONAME, "Identifier expected" },
32 { frxml::E_NOEQ, "Equal sign(=) expected" },
33 { frxml::E_NOQUOTE, "Quote(\", \') expected" },
34 { frxml::E_QUOTENOTCLOSED, "Quote not closed" },
35 { frxml::E_INVCHAR, "Invalid character detected" },
36 { frxml::E_NOTAG, "Tag expected" },
37 { frxml::E_TAGNOTCLOSED, "Tag not closed" },
38 { frxml::E_DUPATTR, "Duplicated attributes detected" },
39 { frxml::E_ELEMNOTCLOSED, "Element not closed" },
40 { frxml::E_INVETAG, "Invalid ETag" }
41};
42
44 : BackendAction(shared::B_CSharp, "c#", "C# backend module")
45{
46}
47
48int lilac::csharp::CSharpBackendAction::Run(std::string confPath, std::string libPath, std::string outPath, std::vector<std::string> argv) const
49{
50 std::stringstream input;
51 try
52 {
53 std::ifstream ifs(confPath);
54 input << ifs.rdbuf();
55 }
56 catch (const std::ifstream::failure& e)
57 {
58 std::cerr << "Error occurred while opening file: " << e.what() << std::endl;
59 return 1;
60 }
61
62 const std::string& source = input.str();
63
64 frxml::doc doc(source);
65 if (!doc)
66 {
67 const char* begin = doc.error().source;
68 size_t length = 1;
69
70 for (auto i = 0; i < 5 && begin > &source[0]; ++i)
71 begin--;
72 for (auto i = 0; i < 5 && begin + length < &source[source.size() - 1]; ++i)
73 length++;
74
75 std::cerr
76 << "Error occurred while parsing XML: "
77 << errorMap[doc.error().code]
78 << " (" << std::string_view(begin, length) << ")\n";
79 }
80
81 try
82 {
83 if (!outPath.ends_with('/'))
84 outPath += "/";
85 outPath += libPath + ".cs";
86 std::ofstream ofs(outPath);
87
88 VisitContext ctx{ "", libPath, ofs };
90 visitor.Begin(ctx, doc.root() /* there is neither parent nor null */, doc.root(), 0);
91 }
92 catch (const shared::exception& e)
93 {
94 e.print();
95 }
96 catch (const std::ofstream::failure& e)
97 {
98 std::cerr << "Error occurred while opening file: " << e.what() << std::endl;
99 return 1;
100 }
101
102 return 0;
103}
104
105[[maybe_unused]]
A visitor that creates bridge of assembly elements with C#.
void Begin(VisitContext &ctx, const frxml::dom &parent, const frxml::dom &current, int depth) override
An action that generates C# bridge of given interface representation using P/Invoke.
Definition module.h:30
int Run(std::string confPath, std::string libPath, std::string outPath, std::vector< std::string > argv) const override
Runs action with given paths.
Definition module.cxx:48
lilac::csharp::CSharpBackendAction Action
Definition module.cxx:106
std::map< int, std::string > errorMap
Definition module.cxx:29