mttFetchApps.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. function model = mttFetchApps(filename)
  2. if ~mttFileExists(filename)
  3. model = [] ;
  4. return ;
  5. end
  6. model.representation = 'apps' ;
  7. mttNotify(['...processing ',filename]) ;
  8. mttWriteNewLine ;
  9. model.source = mttCutText(filename,'_apps.txt') ;
  10. model_name = mttDetachText(model.source,'/') ;
  11. content = mttReadFile(filename) ;
  12. statements = mttExtractStatements(content) ;
  13. number_of_statements = length(statements) ;
  14. apps_declared = 0 ;
  15. next = 0 ;
  16. parsing = 1 ;
  17. while parsing
  18. next = next + 1 ;
  19. statement = statements{next} ;
  20. [keyword,line] = mttSeparateText(statement) ;
  21. switch keyword
  22. case 'apps',
  23. mttAssert(~apps_declared,...
  24. '"apps" declaration must be unique') ;
  25. apps_declared = 1 ;
  26. apps_name = line ;
  27. mttAssert(strcmp(apps_name,model_name),...
  28. ['Wrong name:[',apps_name,'] Expecting:[',model_name,']']) ;
  29. [apps,next] = fetch_apps(statements,next) ;
  30. model = mttAppendFields(model,apps) ;
  31. case '{',
  32. error('Unexpected "{" found') ;
  33. case '}',
  34. error('Unexpected "}" found') ;
  35. otherwise,
  36. error(['Unrecognised top-level keyword "',keyword,'"']) ;
  37. end
  38. if next==number_of_statements
  39. parsing = 0 ;
  40. end
  41. end
  42. function [apps,next] = fetch_apps(statements,next)
  43. apps = [] ;
  44. unit_name = 'apps' ;
  45. number_of_statements = length(statements) ;
  46. open = 0 ;
  47. counter = 0 ;
  48. parsing = 1 ;
  49. while parsing
  50. next = next + 1 ;
  51. statement = statements{next} ;
  52. [keyword,line] = mttSeparateText(statement) ;
  53. switch keyword
  54. case 'app',
  55. mttAssert(open,...
  56. ['"app" declarations must be contained inside {...} in "',unit_name,'"']) ;
  57. app_name = line ;
  58. counter = counter + 1 ;
  59. apps.app{counter} = app_name ;
  60. case '{',
  61. mttAssert(~open,['Unmatched "{" in "',unit_name,'"']) ;
  62. open = 1 ;
  63. case '}',
  64. mttAssert(open,['Unmatched "}" in "',unit_name,'"']) ;
  65. open = 0 ;
  66. otherwise,
  67. error(['Unrecognised_keyword "',keyword,'" in "',unit_name,'"']) ;
  68. end
  69. mttAssert(~(open & (next==number_of_statements)),...
  70. ['Missing "}" in "',unit_name,'"']) ;
  71. if (~open) | (next==number_of_statements)
  72. parsing = 0 ;
  73. end
  74. end
  75. apps = mttSetFieldDefault(apps,'app',[]) ;