aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/python-console/ParseHelper.cpp
blob: 07616f5d7ffc3c2460b7683947cc0dcfff6c049b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
Copyright (c) 2014 Alex Tsui

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "ParseHelper.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>
#include "ParseListener.h"

ParseHelper::Indent::
Indent( )
{ }

ParseHelper::Indent::
Indent( const std::string& indent ):
    Token( indent )
{ }

ParseHelper::ParseState::
ParseState( ParseHelper& parent_ ): parent( parent_ )
{ }

ParseHelper::ParseState::
~ParseState( )
{ }

bool ParseHelper::PeekIndent( const std::string& str, Indent* indent )
{
    if ( !str.size() || ! isspace(str[0]) )
        return false;

    int nonwhitespaceIndex = -1;
    for (size_t i = 0; i < str.size(); ++i)
    {
        if (!isspace(str[i]))
        {
            nonwhitespaceIndex = i;
            break;
        }
    }
    if (nonwhitespaceIndex == -1)
    {
        return false;
    }
    std::string indentToken = str.substr(0, nonwhitespaceIndex);
    indent->Token = indentToken;
    return true;
}

ParseHelper::ParseHelper( )
{ }

void ParseHelper::process( const std::string& str )
{
    std::string top;
    commandBuffer.push_back(str);
    //std::string top = commandBuffer.back();
    //commandBuffer.pop_back();
    std::shared_ptr<ParseState> blockStatePtr;
    while (stateStack.size())
    {
        top = commandBuffer.back();
        commandBuffer.pop_back();
        blockStatePtr = stateStack.back();
        if (blockStatePtr->process(top))
            return;
    }

    if ( ! commandBuffer.size() )
        return;

    // standard state
    top = commandBuffer.back();
    if ( !top.size() )
    {
        reset( );
        broadcast( std::string() );
        return;
    }

    { // check for unexpected indent
        Indent ind;
        bool isIndented = PeekIndent( top, &ind );
        if ( isIndented &&
            ! isInContinuation( ) )
        {
            reset( );
            ParseMessage msg( 1, "IndentationError: unexpected indent");
            broadcast( msg );
            return;
        }
    }

    // enter indented block state
    if ( top[top.size()-1] == ':' )
    {
        std::shared_ptr<ParseState> parseState(
            new BlockParseState( *this ) );
        stateStack.push_back( parseState );
        return;
    }

    if ( top[top.size()-1] == '\\' )
    {
        std::shared_ptr<ParseState> parseState(
            new ContinuationParseState( *this ) );
        stateStack.push_back( parseState );
        return;
    }

    if (BracketParseState::HasOpenBrackets( top ))
    {
        // FIXME: Every parse state should have its own local buffer
        commandBuffer.pop_back( );
        std::shared_ptr<ParseState> parseState(
            new BracketParseState( *this, top ) );
        stateStack.push_back( parseState );
        return;
    }

    // handle single-line statement
    flush( );
}

bool ParseHelper::buffered( ) const
{
    return commandBuffer.size( ) || stateStack.size( );
}

void ParseHelper::flush( )
{
    std::stringstream ss;
    for (size_t i = 0; i < commandBuffer.size(); ++i )
    {
        ss << commandBuffer[i] << "\n";
    }
    commandBuffer.clear();

    broadcast( ss.str() );
    // TODO: feed string to interpreter
}

void ParseHelper::reset( )
{
//    inContinuation = false;
    stateStack.clear( );
    commandBuffer.clear( );
}

bool ParseHelper::isInContinuation( ) const
{
    return (stateStack.size()
        && (std::dynamic_pointer_cast<ContinuationParseState>(
            stateStack.back())));
}

void ParseHelper::subscribe( ParseListener* listener )
{
    listeners.push_back( listener );
}

void ParseHelper::unsubscribeAll( )
{
    listeners.clear( );
}

void ParseHelper::broadcast( const ParseMessage& msg )
{
    // broadcast signal
    for (size_t i = 0; i < listeners.size(); ++i)
    {
        if (listeners[i])
        {
            listeners[i]->parseEvent(msg);
        }
    }
}
>.index_at(index...), a.data(index...)); } template<typename... Ix> arr& mutate_data(arr& a, Ix... index) { auto ptr = (uint8_t *) a.mutable_data(index...); for (py::ssize_t i = 0; i < a.nbytes() - a.offset_at(index...); i++) ptr[i] = (uint8_t) (ptr[i] * 2); return a; } template<typename... Ix> arr_t& mutate_data_t(arr_t& a, Ix... index) { auto ptr = a.mutable_data(index...); for (py::ssize_t i = 0; i < a.size() - a.index_at(index...); i++) ptr[i]++; return a; } template<typename... Ix> py::ssize_t index_at(const arr& a, Ix... idx) { return a.index_at(idx...); } template<typename... Ix> py::ssize_t index_at_t(const arr_t& a, Ix... idx) { return a.index_at(idx...); } template<typename... Ix> py::ssize_t offset_at(const arr& a, Ix... idx) { return a.offset_at(idx...); } template<typename... Ix> py::ssize_t offset_at_t(const arr_t& a, Ix... idx) { return a.offset_at(idx...); } template<typename... Ix> py::ssize_t at_t(const arr_t& a, Ix... idx) { return a.at(idx...); } template<typename... Ix> arr_t& mutate_at_t(arr_t& a, Ix... idx) { a.mutable_at(idx...)++; return a; } #define def_index_fn(name, type) \ sm.def(#name, [](type a) { return name(a); }); \ sm.def(#name, [](type a, int i) { return name(a, i); }); \ sm.def(#name, [](type a, int i, int j) { return name(a, i, j); }); \ sm.def(#name, [](type a, int i, int j, int k) { return name(a, i, j, k); }); template <typename T, typename T2> py::handle auxiliaries(T &&r, T2 &&r2) { if (r.ndim() != 2) throw std::domain_error("error: ndim != 2"); py::list l; l.append(*r.data(0, 0)); l.append(*r2.mutable_data(0, 0)); l.append(r.data(0, 1) == r2.mutable_data(0, 1)); l.append(r.ndim()); l.append(r.itemsize()); l.append(r.shape(0)); l.append(r.shape(1)); l.append(r.size()); l.append(r.nbytes()); return l.release(); } // note: declaration at local scope would create a dangling reference! static int data_i = 42; TEST_SUBMODULE(numpy_array, sm) { try { py::module_::import("numpy"); } catch (...) { return; } // test_dtypes py::class_<DtypeCheck>(sm, "DtypeCheck") .def_readonly("numpy", &DtypeCheck::numpy) .def_readonly("pybind11", &DtypeCheck::pybind11) .def("__repr__", [](const DtypeCheck& self) { return py::str("<DtypeCheck numpy={} pybind11={}>").format( self.numpy, self.pybind11); }); sm.def("get_concrete_dtype_checks", &get_concrete_dtype_checks); py::class_<DtypeSizeCheck>(sm, "DtypeSizeCheck") .def_readonly("name", &DtypeSizeCheck::name) .def_readonly("size_cpp", &DtypeSizeCheck::size_cpp) .def_readonly("size_numpy", &DtypeSizeCheck::size_numpy) .def("__repr__", [](const DtypeSizeCheck& self) { return py::str("<DtypeSizeCheck name='{}' size_cpp={} size_numpy={} dtype={}>").format( self.name, self.size_cpp, self.size_numpy, self.dtype); }); sm.def("get_platform_dtype_size_checks", &get_platform_dtype_size_checks); // test_array_attributes sm.def("ndim", [](const arr& a) { return a.ndim(); }); sm.def("shape", [](const arr& a) { return arr(a.ndim(), a.shape()); }); sm.def("shape", [](const arr& a, py::ssize_t dim) { return a.shape(dim); }); sm.def("strides", [](const arr& a) { return arr(a.ndim(), a.strides()); }); sm.def("strides", [](const arr& a, py::ssize_t dim) { return a.strides(dim); }); sm.def("writeable", [](const arr& a) { return a.writeable(); }); sm.def("size", [](const arr& a) { return a.size(); }); sm.def("itemsize", [](const arr& a) { return a.itemsize(); }); sm.def("nbytes", [](const arr& a) { return a.nbytes(); }); sm.def("owndata", [](const arr& a) { return a.owndata(); }); // test_index_offset def_index_fn(index_at, const arr&); def_index_fn(index_at_t, const arr_t&); def_index_fn(offset_at, const arr&); def_index_fn(offset_at_t, const arr_t&); // test_data def_index_fn(data, const arr&); def_index_fn(data_t, const arr_t&); // test_mutate_data, test_mutate_readonly def_index_fn(mutate_data, arr&); def_index_fn(mutate_data_t, arr_t&); def_index_fn(at_t, const arr_t&); def_index_fn(mutate_at_t, arr_t&); // test_make_c_f_array sm.def("make_f_array", [] { return py::array_t<float>({ 2, 2 }, { 4, 8 }); }); sm.def("make_c_array", [] { return py::array_t<float>({ 2, 2 }, { 8, 4 }); }); // test_empty_shaped_array sm.def("make_empty_shaped_array", [] { return py::array(py::dtype("f"), {}, {}); }); // test numpy scalars (empty shape, ndim==0) sm.def("scalar_int", []() { return py::array(py::dtype("i"), {}, {}, &data_i); }); // test_wrap sm.def("wrap", [](py::array a) { return py::array( a.dtype(), {a.shape(), a.shape() + a.ndim()}, {a.strides(), a.strides() + a.ndim()}, a.data(), a ); }); // test_numpy_view struct ArrayClass { int data[2] = { 1, 2 }; ArrayClass() { py::print("ArrayClass()"); } ~ArrayClass() { py::print("~ArrayClass()"); } }; py::class_<ArrayClass>(sm, "ArrayClass") .def(py::init<>()) .def("numpy_view", [](py::object &obj) { py::print("ArrayClass::numpy_view()"); auto &a = obj.cast<ArrayClass&>(); return py::array_t<int>({2}, {4}, a.data, obj); } ); // test_cast_numpy_int64_to_uint64 sm.def("function_taking_uint64", [](uint64_t) { }); // test_isinstance sm.def("isinstance_untyped", [](py::object yes, py::object no) { return py::isinstance<py::array>(yes) && !py::isinstance<py::array>(no); }); sm.def("isinstance_typed", [](py::object o) { return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o); }); // test_constructors sm.def("default_constructors", []() { return py::dict( "array"_a=py::array(), "array_t<int32>"_a=py::array_t<std::int32_t>(), "array_t<double>"_a=py::array_t<double>() ); }); sm.def("converting_constructors", [](py::object o) { return py::dict( "array"_a=py::array(o), "array_t<int32>"_a=py::array_t<std::int32_t>(o), "array_t<double>"_a=py::array_t<double>(o) ); }); // test_overload_resolution sm.def("overloaded", [](py::array_t<double>) { return "double"; }); sm.def("overloaded", [](py::array_t<float>) { return "float"; }); sm.def("overloaded", [](py::array_t<int>) { return "int"; }); sm.def("overloaded", [](py::array_t<unsigned short>) { return "unsigned short"; }); sm.def("overloaded", [](py::array_t<long long>) { return "long long"; }); sm.def("overloaded", [](py::array_t<std::complex<double>>) { return "double complex"; }); sm.def("overloaded", [](py::array_t<std::complex<float>>) { return "float complex"; }); sm.def("overloaded2", [](py::array_t<std::complex<double>>) { return "double complex"; }); sm.def("overloaded2", [](py::array_t<double>) { return "double"; }); sm.def("overloaded2", [](py::array_t<std::complex<float>>) { return "float complex"; }); sm.def("overloaded2", [](py::array_t<float>) { return "float"; }); // Only accept the exact types: sm.def("overloaded3", [](py::array_t<int>) { return "int"; }, py::arg().noconvert()); sm.def("overloaded3", [](py::array_t<double>) { return "double"; }, py::arg().noconvert()); // Make sure we don't do unsafe coercion (e.g. float to int) when not using forcecast, but // rather that float gets converted via the safe (conversion to double) overload: sm.def("overloaded4", [](py::array_t<long long, 0>) { return "long long"; }); sm.def("overloaded4", [](py::array_t<double, 0>) { return "double"; }); // But we do allow conversion to int if forcecast is enabled (but only if no overload matches // without conversion) sm.def("overloaded5", [](py::array_t<unsigned int>) { return "unsigned int"; }); sm.def("overloaded5", [](py::array_t<double>) { return "double"; }); // test_greedy_string_overload // Issue 685: ndarray shouldn't go to std::string overload sm.def("issue685", [](std::string) { return "string"; }); sm.def("issue685", [](py::array) { return "array"; }); sm.def("issue685", [](py::object) { return "other"; }); // test_array_unchecked_fixed_dims sm.def("proxy_add2", [](py::array_t<double> a, double v) { auto r = a.mutable_unchecked<2>(); for (py::ssize_t i = 0; i < r.shape(0); i++) for (py::ssize_t j = 0; j < r.shape(1); j++) r(i, j) += v; }, py::arg().noconvert(), py::arg()); sm.def("proxy_init3", [](double start) { py::array_t<double, py::array::c_style> a({ 3, 3, 3 }); auto r = a.mutable_unchecked<3>(); for (py::ssize_t i = 0; i < r.shape(0); i++) for (py::ssize_t j = 0; j < r.shape(1); j++) for (py::ssize_t k = 0; k < r.shape(2); k++) r(i, j, k) = start++; return a; }); sm.def("proxy_init3F", [](double start) { py::array_t<double, py::array::f_style> a({ 3, 3, 3 }); auto r = a.mutable_unchecked<3>(); for (py::ssize_t k = 0; k < r.shape(2); k++) for (py::ssize_t j = 0; j < r.shape(1); j++) for (py::ssize_t i = 0; i < r.shape(0); i++) r(i, j, k) = start++; return a; }); sm.def("proxy_squared_L2_norm", [](py::array_t<double> a) { auto r = a.unchecked<1>(); double sumsq = 0; for (py::ssize_t i = 0; i < r.shape(0); i++) sumsq += r[i] * r(i); // Either notation works for a 1D array return sumsq; }); sm.def("proxy_auxiliaries2", [](py::array_t<double> a) { auto r = a.unchecked<2>(); auto r2 = a.mutable_unchecked<2>(); return auxiliaries(r, r2); }); sm.def("proxy_auxiliaries1_const_ref", [](py::array_t<double> a) { const auto &r = a.unchecked<1>(); const auto &r2 = a.mutable_unchecked<1>(); return r(0) == r2(0) && r[0] == r2[0]; }); sm.def("proxy_auxiliaries2_const_ref", [](py::array_t<double> a) { const auto &r = a.unchecked<2>(); const auto &r2 = a.mutable_unchecked<2>(); return r(0, 0) == r2(0, 0); }); // test_array_unchecked_dyn_dims // Same as the above, but without a compile-time dimensions specification: sm.def("proxy_add2_dyn", [](py::array_t<double> a, double v) { auto r = a.mutable_unchecked(); if (r.ndim() != 2) throw std::domain_error("error: ndim != 2"); for (py::ssize_t i = 0; i < r.shape(0); i++) for (py::ssize_t j = 0; j < r.shape(1); j++) r(i, j) += v; }, py::arg().noconvert(), py::arg()); sm.def("proxy_init3_dyn", [](double start) { py::array_t<double, py::array::c_style> a({ 3, 3, 3 }); auto r = a.mutable_unchecked(); if (r.ndim() != 3) throw std::domain_error("error: ndim != 3"); for (py::ssize_t i = 0; i < r.shape(0); i++) for (py::ssize_t j = 0; j < r.shape(1); j++) for (py::ssize_t k = 0; k < r.shape(2); k++) r(i, j, k) = start++; return a; }); sm.def("proxy_auxiliaries2_dyn", [](py::array_t<double> a) { return auxiliaries(a.unchecked(), a.mutable_unchecked()); }); sm.def("array_auxiliaries2", [](py::array_t<double> a) { return auxiliaries(a, a); }); // test_array_failures // Issue #785: Uninformative "Unknown internal error" exception when constructing array from empty object: sm.def("array_fail_test", []() { return py::array(py::object()); }); sm.def("array_t_fail_test", []() { return py::array_t<double>(py::object()); }); // Make sure the error from numpy is being passed through: sm.def("array_fail_test_negative_size", []() { int c = 0; return py::array(-1, &c); }); // test_initializer_list // Issue (unnumbered; reported in #788): regression: initializer lists can be ambiguous sm.def("array_initializer_list1", []() { return py::array_t<float>(1); }); // { 1 } also works, but clang warns about it sm.def("array_initializer_list2", []() { return py::array_t<float>({ 1, 2 }); }); sm.def("array_initializer_list3", []() { return py::array_t<float>({ 1, 2, 3 }); }); sm.def("array_initializer_list4", []() { return py::array_t<float>({ 1, 2, 3, 4 }); }); // test_array_resize // reshape array to 2D without changing size sm.def("array_reshape2", [](py::array_t<double> a) { const auto dim_sz = (py::ssize_t)std::sqrt(a.size()); if (dim_sz * dim_sz != a.size()) throw std::domain_error("array_reshape2: input array total size is not a squared integer"); a.resize({dim_sz, dim_sz}); }); // resize to 3D array with each dimension = N sm.def("array_resize3", [](py::array_t<double> a, size_t N, bool refcheck) { a.resize({N, N, N}, refcheck); }); // test_array_create_and_resize // return 2D array with Nrows = Ncols = N sm.def("create_and_resize", [](size_t N) { py::array_t<double> a; a.resize({N, N}); std::fill(a.mutable_data(), a.mutable_data() + a.size(), 42.); return a; }); sm.def("index_using_ellipsis", [](py::array a) { return a[py::make_tuple(0, py::ellipsis(), 0)]; }); // test_argument_conversions sm.def("accept_double", [](py::array_t<double, 0>) {}, py::arg("a")); sm.def("accept_double_forcecast", [](py::array_t<double, py::array::forcecast>) {}, py::arg("a")); sm.def("accept_double_c_style", [](py::array_t<double, py::array::c_style>) {}, py::arg("a")); sm.def("accept_double_c_style_forcecast", [](py::array_t<double, py::array::forcecast | py::array::c_style>) {}, py::arg("a")); sm.def("accept_double_f_style", [](py::array_t<double, py::array::f_style>) {}, py::arg("a")); sm.def("accept_double_f_style_forcecast", [](py::array_t<double, py::array::forcecast | py::array::f_style>) {}, py::arg("a")); sm.def("accept_double_noconvert", [](py::array_t<double, 0>) {}, py::arg("a").noconvert()); sm.def("accept_double_forcecast_noconvert", [](py::array_t<double, py::array::forcecast>) {}, py::arg("a").noconvert()); sm.def("accept_double_c_style_noconvert", [](py::array_t<double, py::array::c_style>) {}, py::arg("a").noconvert()); sm.def("accept_double_c_style_forcecast_noconvert", [](py::array_t<double, py::array::forcecast | py::array::c_style>) {}, py::arg("a").noconvert()); sm.def("accept_double_f_style_noconvert", [](py::array_t<double, py::array::f_style>) {}, py::arg("a").noconvert()); sm.def("accept_double_f_style_forcecast_noconvert", [](py::array_t<double, py::array::forcecast | py::array::f_style>) {}, py::arg("a").noconvert()); }