Goose  Diff

Differences From Artifact [fe6535224f]:

  • File bs/lexer/lexer.cpp — part of check-in [2b48ab1ac7] at 2019-01-13 14:45:53 on branch trunk — Lexer: integer literals. (user: achavasse size: 2967)

To Artifact [2d8573f259]:

  • File bs/lexer/lexer.cpp — part of check-in [bc31b6de5e] at 2019-01-13 14:57:35 on branch trunk — Lexer: alphanumeric identifiers. (user: achavasse size: 3413)

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
            continue;
        }

        return;
    }
}

Term Lexer::readStringLiteral( ir::Location&& loc )
{
    string str;
    bool complete = false;

    // TODO this is very simplistic, at some point
    // we'll want to handle escape characters etc.
    while( m_input.good() )







|







49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
            continue;
        }

        return;
    }
}

Term Lexer::readStringLiteral( Location&& loc )
{
    string str;
    bool complete = false;

    // TODO this is very simplistic, at some point
    // we'll want to handle escape characters etc.
    while( m_input.good() )
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
            newLine();
            break;
        }

        str += c;
    }

    ir::Term result( move( loc ), str );

    if( !complete )
    {
        cout << result.location() << ": unterminated string literal.\n";
        result.setFaulty();
    }

    return result;
}

Term Lexer::readIntegerLiteral( ir::Location&& loc )
{
    uint64_t val = 0;

    while( m_input.good() )
    {
        auto c = m_input.peek();
        if( !isdigit( c ) )
            break;


        val = val * 10 + c - '0';
    }

    ir::Term result( move( loc ), val );


















    return result;
}

optional< Term > Lexer::readToken()
{
    skipSpacingIfAny();
    if( !m_input.good() )
        return nullopt;

    ir::Location loc( m_filename, m_lineNum, m_input.tellg() - m_lastLineBreakOffset );

    auto c = m_input.peek();

    switch( c )
    {
        case '{':
            m_input.get();
            return ir::Term( move( loc ), "{"_sid );

        case '}':
            m_input.get();
            return ir::Term( move( loc ), "}"_sid );

        case '(':
            m_input.get();
            return ir::Term( move( loc ), "("_sid );

        case ')':
            m_input.get();
            return ir::Term( move( loc ), ")"_sid );

        case '[':
            m_input.get();
            return ir::Term( move( loc ), "["_sid );

        case ']':
            m_input.get();
            return ir::Term( move( loc ), "]"_sid );

        case '"':
            m_input.get();
            return readStringLiteral( move( loc ) );
    }

    if( isdigit( c ) )
        return readIntegerLiteral( move( loc ) );





    return nullopt;
}







|










|









>



|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









|







|



|



|



|



|



|









>
>
>
>


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
            newLine();
            break;
        }

        str += c;
    }

    Term result( move( loc ), str );

    if( !complete )
    {
        cout << result.location() << ": unterminated string literal.\n";
        result.setFaulty();
    }

    return result;
}

Term Lexer::readIntegerLiteral( Location&& loc )
{
    uint64_t val = 0;

    while( m_input.good() )
    {
        auto c = m_input.peek();
        if( !isdigit( c ) )
            break;

        m_input.get();
        val = val * 10 + c - '0';
    }

    Term result( move( loc ), val );
    return result;
}

Term Lexer::readAlphanumericIdentifier( Location&& loc )
{
    string str;

    while( m_input.good() )
    {
        auto c = m_input.peek();
        if( !isalpha( c ) && !isdigit( c ) && c != '_' )
            break;

        m_input.get();
        str += c;
    }

    Term result( move( loc ), str );
    return result;
}

optional< Term > Lexer::readToken()
{
    skipSpacingIfAny();
    if( !m_input.good() )
        return nullopt;

    Location loc( m_filename, m_lineNum, m_input.tellg() - m_lastLineBreakOffset );

    auto c = m_input.peek();

    switch( c )
    {
        case '{':
            m_input.get();
            return Term( move( loc ), "{"_sid );

        case '}':
            m_input.get();
            return Term( move( loc ), "}"_sid );

        case '(':
            m_input.get();
            return Term( move( loc ), "("_sid );

        case ')':
            m_input.get();
            return Term( move( loc ), ")"_sid );

        case '[':
            m_input.get();
            return Term( move( loc ), "["_sid );

        case ']':
            m_input.get();
            return Term( move( loc ), "]"_sid );

        case '"':
            m_input.get();
            return readStringLiteral( move( loc ) );
    }

    if( isdigit( c ) )
        return readIntegerLiteral( move( loc ) );

    if( isalpha( c ) || c == '_' )
        return readAlphanumericIdentifier( move( loc ) );

    cout << loc << ": unrecognized token.\n";
    return nullopt;
}