chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace OpenTap
{
    internal class LicenseParser
    {
        internal static LicenseBase ParseString(string licenseString)
        {
            Scanner sc = new Scanner(licenseString);
 
            if (sc.Tokens.Any(t => t.Token == LicenseToken.Error))
                throw new Exception(string.Format("Error while parsing license string at position {0}.", sc.Tokens.First(t => t.Token == LicenseToken.Error).Position));
 
            var license = ParseLicensesAny(sc.Tokens);
 
            if (sc.Tokens.Count > 0)
                throw new Exception(string.Format("Invalid license string. Unepxected token: {0}", sc.Tokens[0].Token));
 
            return license;
 
            /*
                Rule = LicensesAny .
                LicensesAny = LicensesAll { '| LicensesAll } .
                LicensesAll = License { '&' License } .
                License = Identifier | Process | "<Any Identifier>" | '(' LicensesAny ')' .
             */
        }
 
        private static LicenseBase ParseLicensesAny(List<TokenEntry> tokens)
        {
            LicenseBase la = ParseLicensesAll(tokens);
 
            if ((tokens.Count > 0) && (tokens[0].Token == LicenseToken.Or))
            {
                var lic = new List<LicenseBase>() { la };
 
                while ((tokens.Count > 0) && (tokens[0].Token == LicenseToken.Or))
                {
                    tokens.RemoveAt(0);
 
                    lic.Add(ParseLicensesAll(tokens));
                }
 
                return new LicenseAny(lic.ToArray());
            }
            else
                return la;
        }
 
        private static LicenseBase ParseLicensesAll(List<TokenEntry> tokens)
        {
            LicenseBase la = ParseLicense(tokens);
 
            if ((tokens.Count > 0) && (tokens[0].Token == LicenseToken.And))
            {
                var lic = new List<LicenseBase>() { la };
 
                while ((tokens.Count > 0) && (tokens[0].Token == LicenseToken.And))
                {
                    tokens.RemoveAt(0);
 
                    lic.Add(ParseLicense(tokens));
                }
 
                return new LicenseAll(lic.ToArray());
            }
            else
                return la;
        }
 
        private static LicenseBase ParseLicense(List<TokenEntry> tokens)
        {
            if (tokens.Count <= 0) throw new Exception("Failed to get license.");
 
            if (tokens[0].Token == LicenseToken.Identifier)
            {
                var t = tokens[0];
                tokens.RemoveAt(0);
                return new LicenseRequired(t.Text);
            }
            else if (tokens[0].Token == LicenseToken.Process)
            {
                var t = tokens[0];
                tokens.RemoveAt(0);
                return new LicenseProcess(t.Text);
            }
            else if (tokens[0].Token == LicenseToken.LParan)
            {
                tokens.RemoveAt(0);
                var t = ParseLicensesAny(tokens);
                tokens.RemoveAt(0);
 
                return t;
            }
            else
                throw new Exception("Failed to parse license string.");
        }
    }
}