1+ # -----------------------------------------------------------------------
2+ # This file is part of MoonScript
3+ #
4+ # MoonSript is free software: you can redistribute it and/or modify
5+ # it under the terms of the GNU General Public License as published by
6+ # the Free Software Foundation, either version 3 of the License, or
7+ # (at your option) any later version.
8+ #
9+ # MoonSript is distributed in the hope that it will be useful,
10+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ # GNU General Public License for more details.
13+ #
14+ # You should have received a copy of the GNU General Public License
15+ # along with MoonSript. If not, see <https://www.gnu.org/licenses/>.
16+ #
17+ # Copyright (C) 2025 Krisna Pranav, MoonScript Developers
18+ # -----------------------------------------------------------------------
19+
20+ module MoonScript
21+ class MoonJson
22+ class Parser
23+ include Errorable
24+ extend Errorable
25+
26+ def self.parse (* , contents: String , path: String ) : MoonJson
27+ new(contents: contents, path: path).parse
28+ rescue exception: JSON ::ParseException
29+ error! :invalid_json do
30+ end
31+ end
32+
33+ def self.parse (path: String , * , search: Bool = false ) : MoonJson
34+ file = path
35+
36+ if search
37+ error! :moon_json_not_found do
38+ end
39+ end
40+ end
41+
42+ def initialize (* , @contents : String , @path : String )
43+ @parser = JSON ::PullParser .new(@contents )
44+ end
45+
46+ def self.snippet_data (* , column_number: Int32 , line_number: Int32 , contents: String , path: String )
47+ position = if line_number - 1 == 0 else contents.lines[0 ..line_number - 2 ]
48+
49+ Error ::Snippetdata .new(to: position + 1 )
50+ end
51+
52+ def snippet_data
53+ snippet_data @parser .location
54+ end
55+
56+ def root
57+ File .dirname(@path )
58+ end
59+ end
60+ end
61+ end
0 commit comments