LLOOP Index | GSP Language | GSP Library | Framework Classes | Component Classes

file.gsp

This is the verbatim text of the file "file.gsp" part of the LLOOP package. The copyright remains with Michel MEHL. All rights reserved.



/**
 * This defines several tokens related to files:
 * - file: reads a file name or path, either UNIX or Windows
 *
 * - existingfile: same as file, except that the file must exist
 *
 * - directory: same as file, except that the file must be a valid directory if it exists
 *
 * - existingdir: same as directory, except the directory must exist and must be a directory
 *
 * - forcedir: same as directory, except the directory is created if it does not exist. An error is raised if it already exists and it is not a directory.
 */

import fileunix;
import filewindows;

symbol FileToken
  extends class universal::File()
  include <stdio.h>, 
          <ctype.h>, 
           "universal__File.h"
  alias "file";

symbol ExistingFileToken 
  extends symbol FileToken()
  alias "existingfile"
  test  
  {{ 
    "."  : fail "."
  }}  
;

symbol ForceDirectoryToken 
  extends symbol FileToken()
  alias "forcedirectory", "forcedir"
  test  
  {{ 
    "."  : pass "."
    ".." : pass ".."
  }}  
;

symbol DirectoryToken 
  extends symbol FileToken()
  alias "directory", "dir"
  test  
  {{ 
    "."  : pass "."
    ".." : pass ".."
    "..." : pass "..."
  }}  
;

symbol ExistingDirectoryToken 
  extends symbol FileToken()
  alias "existingdirectory", "existingdir"
  test  
  {{ 
    "."  : pass "."
    ".." : pass ".."
    "??" : fail "??"
  }}  
;

FileToken ::= fileunix
{{
 /* By default the file token takes the same file system category 
  * as the unix file token.
  * If the current platform is windows and the file could also be parsed 
  * as a valid windows file, e.g '...', the file system category is forced to 'windows'
  */

  m_fs = $1.fileSystemCategory();

#ifdef WIN32
  strstream ss;
  strstream sserr;
  ss << $1.name() << ends;
  FileWindowsToken f;
  if (f.parse(ss, sserr, NULL, false, /* No EOF expected */ true /* Deactivate any preprocessing */))
	{
	  m_fs = f.fileSystemCategory();
	}
#endif

  setName($1.name());
}}
| filewindows
{{
 /* By default the file token takes the same file system category 
  * as the unix file token.
  * If the current platform is windows and the file could also be parsed 
  * as a valid windows file, e.g '...', the file system category is forced to 'windows'
  */

  m_fs = $1.fileSystemCategory();

#ifndef WIN32
  strstream ss;
  strstream sserr;
  ss << $1.name() << ends;
  FileUnixToken f;
  if (f.parse(ss, sserr, NULL, false, /* No EOF expected */ true /* Deactivate any preprocessing */))
	{
	  m_fs = f.fileSystemCategory();
	}
#endif

  setName($1.name());
}}

ExistingFileToken ::= FileToken
{{  
  m_fs = $1.fileSystemCategory();
  setName($1.name());

  if (!exists())
  {
    error (cerr, $1.getLineNo(), "file '%s' does not exist", $1.name().str());
  }
  else if (isDirectory())
  {
    error (cerr, $1.getLineNo(), "'%s' is a directory", $1.name().str());
  }
}}

ExistingDirectoryToken ::= FileToken
{{  
  m_fs = $1.fileSystemCategory();
  setName($1.name());

  if (!exists())
  {
   error (cerr, $1.getLineNo(), "directory '%s' does not exist", $1.name().str());
  }
  else if (!isDirectory())
  {
    error (cerr, $1.getLineNo(), "'%s' is not a directory", $1.name().str());
  }
}}

DirectoryToken ::= FileToken
{{  
  m_fs = $1.fileSystemCategory();
  setName($1.name());

  if (exists() && (!isDirectory()))
  {
    error (cerr, $1.getLineNo(), "'%s' exists and is not a directory", $1.name().str());
  }
}}


ForceDirectoryToken ::= FileToken
{{  
  m_fs = $1.fileSystemCategory();
  setName($1.name());

  if (exists() && (!isDirectory()))
  {
    error (cerr, $1.getLineNo(), "'%s' exists and is not a directory", $1.name().str());
  }
  else if (!exists())
  {
    if (!mkdirs())
    {
      error (cerr, $1.getLineNo(), "failed to create directory '%s'", $1.name().str());
    }
  }
}}


This file is part of the LLOOP Reversible Object-Oriented Parser Generator. Copyright (c) 2005-2006 Michel MEHL, France. All rights reserved. LLOOP is distributed by the company ERSA SaRL.


Copyright (c) 2005-2006 Michel MEHL, Haguenau, France
LLOOP version 1.1