| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <basic/sberrors.hxx> |
| 21 |
#include <parser.hxx> |
| 22 |
#include <iosys.hxx> |
| 23 |
#include <memory> |
| 24 |
|
| 25 |
// test if there's an I/O channel |
| 26 |
|
| 27 |
bool SbiParser::Channel( bool bAlways ) |
| 28 |
{ |
| 29 |
bool bRes = false; |
| 30 |
Peek(); |
| 31 |
if( IsHash() ) |
| 32 |
{ |
| 33 |
SbiExpression aExpr( this ); |
| 34 |
while( Peek() == COMMA || Peek() == SEMICOLON ) |
| 35 |
Next(); |
| 36 |
aExpr.Gen(); |
| 37 |
aGen.Gen( SbiOpcode::CHANNEL_ ); |
| 38 |
bRes = true; |
| 39 |
} |
| 40 |
else if( bAlways ) |
| 41 |
Error( ERRCODE_BASIC_EXPECTED, u"#"_ustr ); |
| 42 |
return bRes; |
| 43 |
} |
| 44 |
|
| 45 |
// it's tried that at object variables the Default- |
| 46 |
// Property is addressed for PRINT and WRITE |
| 47 |
|
| 48 |
void SbiParser::Print() |
| 49 |
{ |
| 50 |
bool bChan = Channel(); |
| 51 |
|
| 52 |
while( !bAbort ) |
| 53 |
{ |
| 54 |
if( !IsEoln( Peek() ) ) |
| 55 |
{ |
| 56 |
SbiExpression(this).Gen(); |
| 57 |
Peek(); |
| 58 |
aGen.Gen( eCurTok == COMMA ? SbiOpcode::PRINTF_ : SbiOpcode::BPRINT_ ); |
| 59 |
} |
| 60 |
if( eCurTok == COMMA || eCurTok == SEMICOLON ) |
| 61 |
{ |
| 62 |
Next(); |
| 63 |
if( IsEoln( Peek() ) ) break; |
| 64 |
} |
| 65 |
else |
| 66 |
{ |
| 67 |
aGen.Gen( SbiOpcode::PRCHAR_, '\n' ); |
| 68 |
break; |
| 69 |
} |
| 70 |
} |
| 71 |
if( bChan ) |
| 72 |
aGen.Gen( SbiOpcode::CHAN0_ ); |
| 73 |
} |
| 74 |
|
| 75 |
// WRITE #chan, expr, ... |
| 76 |
|
| 77 |
void SbiParser::Write() |
| 78 |
{ |
| 79 |
bool bChan = Channel(); |
| 80 |
|
| 81 |
while( !bAbort ) |
| 82 |
{ |
| 83 |
SbiExpression(this).Gen(); |
| 84 |
aGen.Gen( SbiOpcode::BWRITE_ ); |
| 85 |
if( Peek() == COMMA ) |
| 86 |
{ |
| 87 |
aGen.Gen( SbiOpcode::PRCHAR_, ',' ); |
| 88 |
Next(); |
| 89 |
if( IsEoln( Peek() ) ) break; |
| 90 |
} |
| 91 |
else |
| 92 |
{ |
| 93 |
aGen.Gen( SbiOpcode::PRCHAR_, '\n' ); |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
if( bChan ) |
| 98 |
aGen.Gen( SbiOpcode::CHAN0_ ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
// #i92642 Handle LINE keyword outside ::Next() |
| 103 |
void SbiParser::Line() |
| 104 |
{ |
| 105 |
// #i92642: Special handling to allow name as symbol |
| 106 |
if( Peek() == INPUT ) |
| 107 |
{ |
| 108 |
Next(); |
| 109 |
LineInput(); |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
aGen.Statement(); |
| 114 |
|
| 115 |
KeywordSymbolInfo aInfo; |
| 116 |
aInfo.m_aKeywordSymbol = "line"; |
| 117 |
aInfo.m_eSbxDataType = GetType(); |
| 118 |
|
| 119 |
Symbol( &aInfo ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
// LINE INPUT [prompt], var$ |
| 125 |
|
| 126 |
void SbiParser::LineInput() |
| 127 |
{ |
| 128 |
Channel( true ); |
| 129 |
{ |
| 130 |
SbiExpression aExpr(this, SbOPERAND); |
| 131 |
if (!aExpr.IsVariable()) |
| 132 |
Error(ERRCODE_BASIC_VAR_EXPECTED); |
| 133 |
if (aExpr.GetType() != SbxVARIANT && aExpr.GetType() != SbxSTRING) |
| 134 |
Error(ERRCODE_BASIC_CONVERSION); |
| 135 |
aExpr.Gen(); |
| 136 |
aGen.Gen(SbiOpcode::LINPUT_); |
| 137 |
} |
| 138 |
aGen.Gen( SbiOpcode::CHAN0_ ); // ResetChannel() not in StepLINPUT() anymore |
| 139 |
} |
| 140 |
|
| 141 |
// INPUT |
| 142 |
|
| 143 |
void SbiParser::Input() |
| 144 |
{ |
| 145 |
aGen.Gen( SbiOpcode::RESTART_ ); |
| 146 |
Channel( true ); |
| 147 |
while( !bAbort ) |
| 148 |
{ |
| 149 |
SbiExpression aExpr(this, SbOPERAND); |
| 150 |
if (!aExpr.IsVariable()) |
| 151 |
Error( ERRCODE_BASIC_VAR_EXPECTED ); |
| 152 |
aExpr.Gen(); |
| 153 |
aGen.Gen( SbiOpcode::INPUT_ ); |
| 154 |
if (Peek() != COMMA) |
| 155 |
break; |
| 156 |
Next(); |
| 157 |
} |
| 158 |
aGen.Gen( SbiOpcode::CHAN0_ ); |
| 159 |
} |
| 160 |
|
| 161 |
// OPEN stringexpr FOR mode ACCESS access mode AS Channel [Len=n] |
| 162 |
|
| 163 |
void SbiParser::Open() |
| 164 |
{ |
| 165 |
bInStatement = true; |
| 166 |
SbiExpression aFileName( this ); |
| 167 |
SbiToken eTok; |
| 168 |
TestToken( FOR ); |
| 169 |
StreamMode nMode = StreamMode::NONE; |
| 170 |
SbiStreamFlags nFlags = SbiStreamFlags::NONE; |
| 171 |
switch( Next() ) |
| 172 |
{ |
| 173 |
case INPUT: |
| 174 |
nMode = StreamMode::READ; nFlags |= SbiStreamFlags::Input; break; |
| 175 |
case OUTPUT: |
| 176 |
nMode = StreamMode::WRITE | StreamMode::TRUNC; nFlags |= SbiStreamFlags::Output; break; |
| 177 |
case APPEND: |
| 178 |
nMode = StreamMode::WRITE; nFlags |= SbiStreamFlags::Append; break; |
| 179 |
case RANDOM: |
| 180 |
nMode = StreamMode::READ | StreamMode::WRITE; nFlags |= SbiStreamFlags::Random; break; |
| 181 |
case BINARY: |
| 182 |
nMode = StreamMode::READ | StreamMode::WRITE; nFlags |= SbiStreamFlags::Binary; break; |
| 183 |
default: |
| 184 |
Error( ERRCODE_BASIC_SYNTAX ); |
| 185 |
} |
| 186 |
if( Peek() == ACCESS ) |
| 187 |
{ |
| 188 |
Next(); |
| 189 |
eTok = Next(); |
| 190 |
// influence only READ,WRITE-Flags in nMode |
| 191 |
nMode &= ~StreamMode(StreamMode::READ | StreamMode::WRITE); // delete |
| 192 |
if( eTok == READ ) |
| 193 |
{ |
| 194 |
if( Peek() == WRITE ) |
| 195 |
{ |
| 196 |
Next(); |
| 197 |
nMode |= StreamMode::READ | StreamMode::WRITE; |
| 198 |
} |
| 199 |
else |
| 200 |
nMode |= StreamMode::READ; |
| 201 |
} |
| 202 |
else if( eTok == WRITE ) |
| 203 |
nMode |= StreamMode::WRITE; |
| 204 |
else |
| 205 |
Error( ERRCODE_BASIC_SYNTAX ); |
| 206 |
} |
| 207 |
switch( Peek() ) |
| 208 |
{ |
| 209 |
case SHARED: |
| 210 |
Next(); nMode |= StreamMode::SHARE_DENYNONE; break; |
| 211 |
case LOCK: |
| 212 |
Next(); |
| 213 |
eTok = Next(); |
| 214 |
if( eTok == READ ) |
| 215 |
{ |
| 216 |
if( Peek() == WRITE ) |
| 217 |
{ |
| 218 |
Next(); |
| 219 |
nMode |= StreamMode::SHARE_DENYALL; |
| 220 |
} |
| 221 |
else nMode |= StreamMode::SHARE_DENYREAD; |
| 222 |
} |
| 223 |
else if( eTok == WRITE ) |
| 224 |
nMode |= StreamMode::SHARE_DENYWRITE; |
| 225 |
else |
| 226 |
Error( ERRCODE_BASIC_SYNTAX ); |
| 227 |
break; |
| 228 |
default: break; |
| 229 |
} |
| 230 |
TestToken( AS ); |
| 231 |
// channel number |
| 232 |
SbiExpression aChan( this ); |
| 233 |
std::unique_ptr<SbiExpression> pLen; |
| 234 |
if( Peek() == SYMBOL ) |
| 235 |
{ |
| 236 |
Next(); |
| 237 |
if( aSym.equalsIgnoreAsciiCase("LEN") ) |
| 238 |
{ |
| 239 |
TestToken( EQ ); |
| 240 |
pLen.reset(new SbiExpression( this )); |
| 241 |
} |
| 242 |
} |
| 243 |
if( !pLen ) pLen.reset(new SbiExpression( this, 128, SbxINTEGER )); |
| 244 |
// the stack for the OPEN command looks as follows: |
| 245 |
// block length |
| 246 |
// channel number |
| 247 |
// file name |
| 248 |
pLen->Gen(); |
| 249 |
aChan.Gen(); |
| 250 |
aFileName.Gen(); |
| 251 |
aGen.Gen( SbiOpcode::OPEN_, static_cast<sal_uInt32>(nMode), static_cast<sal_uInt32>(nFlags) ); |
| 252 |
bInStatement = false; |
| 253 |
} |
| 254 |
|
| 255 |
// NAME file AS file |
| 256 |
|
| 257 |
void SbiParser::Name() |
| 258 |
{ |
| 259 |
// #i92642: Special handling to allow name as symbol |
| 260 |
if( Peek() == EQ ) |
| 261 |
{ |
| 262 |
aGen.Statement(); |
| 263 |
|
| 264 |
KeywordSymbolInfo aInfo; |
| 265 |
aInfo.m_aKeywordSymbol = "name"; |
| 266 |
aInfo.m_eSbxDataType = GetType(); |
| 267 |
|
| 268 |
Symbol( &aInfo ); |
| 269 |
return; |
| 270 |
} |
| 271 |
SbiExpression aExpr1( this ); |
| 272 |
TestToken( AS ); |
| 273 |
SbiExpression aExpr2( this ); |
| 274 |
aExpr1.Gen(); |
| 275 |
aExpr2.Gen(); |
| 276 |
aGen.Gen( SbiOpcode::RENAME_ ); |
| 277 |
} |
| 278 |
|
| 279 |
// CLOSE [n,...] |
| 280 |
|
| 281 |
void SbiParser::Close() |
| 282 |
{ |
| 283 |
Peek(); |
| 284 |
if( IsEoln( eCurTok ) ) |
| 285 |
aGen.Gen( SbiOpcode::CLOSE_, 0 ); |
| 286 |
else |
| 287 |
for( ;; ) |
| 288 |
{ |
| 289 |
SbiExpression aExpr( this ); |
| 290 |
while( Peek() == COMMA || Peek() == SEMICOLON ) |
| 291 |
Next(); |
| 292 |
aExpr.Gen(); |
| 293 |
aGen.Gen( SbiOpcode::CHANNEL_ ); |
| 294 |
aGen.Gen( SbiOpcode::CLOSE_, 1 ); |
| 295 |
|
| 296 |
if( IsEoln( Peek() ) ) |
| 297 |
break; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |