|
| 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 | + def parse_moon_version : Nil |
| 24 | + location = |
| 25 | + @parser.location |
| 26 | + |
| 27 | + raw = |
| 28 | + @parser.read_string |
| 29 | + |
| 30 | + match = |
| 31 | + raw.match(/(\d+\.\d+\.\d+)\s*<=\s*v\s*<\s*(\d+\.\d+\.\d+)/) |
| 32 | + |
| 33 | + constraint = |
| 34 | + if match |
| 35 | + lower = |
| 36 | + Installer::Semver.parse?(match[1]) |
| 37 | + |
| 38 | + upper = |
| 39 | + Installer::Semver.parse?(match[2]) |
| 40 | + |
| 41 | + Installer::SimpleConstraint.new(lower, upper) if upper && lower |
| 42 | + end |
| 43 | + |
| 44 | + error! :moon_version_bad do |
| 45 | + block do |
| 46 | + text "The" |
| 47 | + bold "moon-version" |
| 48 | + text "constraint should be in this format:" |
| 49 | + end |
| 50 | + |
| 51 | + snippet "0.0.0 <= v < 1.0.0" |
| 52 | + snippet "It is here:", snippet_data(location) |
| 53 | + end unless constraint |
| 54 | + |
| 55 | + resolved = |
| 56 | + Installer::Semver.parse(Moon.version.rchop("-devel")) |
| 57 | + |
| 58 | + error! :moon_version_mismatch do |
| 59 | + block do |
| 60 | + text "The" |
| 61 | + bold "moon-version" |
| 62 | + text "field does not match this version of Moon." |
| 63 | + end |
| 64 | + |
| 65 | + snippet "Expected: ", constraint.to_s |
| 66 | + snippet "but found instead:", Moon.version |
| 67 | + |
| 68 | + snippet "already exists: ", snippet_data(location) |
| 69 | + end unless resolved < constraint.upper && resolved >= constraint.lower |
| 70 | + rescue JSON::ParseException |
| 71 | + error! :moon_version_invalid do |
| 72 | + block do |
| 73 | + text "The" |
| 74 | + bold "moon-version" |
| 75 | + text "field should be a string: " |
| 76 | + end |
| 77 | + |
| 78 | + snippet snippet_data |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | +end |
0 commit comments